"1,2,3"变成[{content:1},{content:2},{content:3}]
第一种写法
let str = "1,2,3";
let arr = [];
let s1 = str.split(',');
s1.map((item) => {
let obj = {};
obj.content = item;
arr.push(obj);
})
console.log(arr)
第二种写法
let str = "1,2,3"
let arr = str.split(",").map((item)=>{
for(let i = 0;i<str.length;i++){
let obj = {};
obj.content = item;
return obj
}
})
console.log(arr) //打印结果如下
第三种写法
let str = "1,2,3";
let arr = str.split(",");
for(let i = 0;i<arr.length;i++){
arr[i] = { content: arr[i]};
}
console.log(arr)