将对象数组中指定键值赋给另一个数组并对更改对象数组中的key值(键名)

let arr = [{id:1, weChat:'test1'},{id:2, weChat:'test2'},{id:3, weChat:'test3'}]
let newArr = [];

arr.forEach((item)=>{
    let obj = {};
    for(let i=0; i <= arr.length; i++){
        obj.id = item.id;
        obj.name = item.weChat;
    }
    newArr.push(obj);
});
console.info(newArr);
//[{id:1, name:'test1'},{id:2, name:'test2'},{id:3, name:'test3'}]

 

你可能感兴趣的:(forEach,循环遍历,Array,操作,对象数组,更改键名,循环遍历)