其他分享
首页 > 其他分享> > 数组对象添加属性

数组对象添加属性

作者:互联网

如何给一个数组对象的每一项新增一个属性 如:isSelected:true??

let newArr = arr.map(v=>{
      return {...v,isSelected:true}
})
let array = [{name: 'lili'}, {name:'petrel'}]
Array.from(array, item => item.isSelected = false)
console.log(array);
数组对象A,数组对象B,A中item.id === B中的item.id的对象新增属性isSelected:false,否则新增属性isSelected:true

const A=[{id: 1},{id:2}, {id: 4}]; const B=[{id: 1}, {id:2}, {id:3}]; const result = A.map(item => { return { ...item, isSelected: B.filter(i=>{ return item.id === i.id }).length > 0 } }) console.log(result)

标签:return,isSelected,item,添加,数组,array,id,属性
来源: https://www.cnblogs.com/li-sir/p/12195499.html