如何给一个数组对象的每一项新增一个属性 如: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)