方法一,使用es6拓展运算符
//向对象数组中添加新的属性 let arr = [{id:1,name:'xiaohua'},{id:2,name:'xiaohong'},] let newArr = arr.map(v=>{ return {...v,editable:false} }) console.log(arr);//不改变原数组,...将每个对象序列化 console.log(newArr ) //[{id:1,name:'xiaohua',editable:false},{id:2,name:'xiaohong',editable:false},]
类同方法一
let person =[{ id: 1, name: 'vhen' },{ id: 2, name: 'json' }] let newArr = obj.map((item,index) =>{ return Object.assign(item,{editable:false}) })
推荐方法一
方法二 使用Object.agess(xxx).forEach
Object.keys(dataList).forEach(item => { console.log(item) // 索引 dataList[item].editable = false })