在数组对象中添加对象属性
方法一:
let list = [{id:'1'},{id:'2'}] let kvlist = []//用来存放组装后新的数组对象 let one = {}//添加的对象 for(var i in list) { one = list[i] one['flag'] = false kvlist.push(one) // console.log(one) } console.log(kvlist) //{[id:'1',flag:false],[id:'2',flag:false]}
方法二:
let arr = [{name:'a'}, {name:'b'}] let arrAfter = []////用来存放组装后新的数组对象 arr.forEach(item => { item.flag = false arrAfter.push(item) }) console.log(arrAfter); //{[name:'1',flag:false],[name:'2',flag:false]}
拓展:
改变数组中的key,value:
let kvlist = [] let one = {} for(var i in list) { one = list[i] one['key'] = list[i].id one['value'] = list[i].name kvlist.push(one) // console.log(one) } console.log(kvlist) _this.bnlist = kvlist
只有在泥泞的道路上才能留下脚印