将 str = “1,2,3”, str2 =“ann,lili,john”

转为:arr = [{id: 1,name: ‘ann’},{id: 2, name: ‘lili’},{id: 3,name: ‘john’}];

代码如下:

let arr = [];
let s1 = str.split(',');
s.forEach((item, i)=>{
let obj = {};
obj.id = item;
arr.push(obj)
})
let s2 = str2.split(',')
s2.forEach((item, i)=>{
arr[i].name = item;
})
原生JS 使用Map将 字符串数组 转 对象数组

let test=["item1","item2","item3"];

利用map转为对象数组
test=test.map((item,index)=>{
return{
key:index,
value:item
}
});

js把字符串转为数组对象_javascript