我有两个数组。
let arr1 = [{id: 1, favorite: false}, {id: 2, favorite: false}, {id: 3, favorite: false}];
let arr2 = [1, 3];
如果ID存在于中arr2
,我想更改favorite
为true
(该特定对象的)
例如。在上述情况下,决赛arr1
看起来像
[{id: 1, favorite: true}, {id: 2, favorite: false}, {id: 3, favorite: true}];
您可以.map
用来遍历arr1
并.includes
检查项目id
是否在arr2
:
const arr1 = [
{id: 1, favorite: false},
{id: 2, favorite: false},
{id: 3, favorite: false}
];
const arr2 = [1, 3];
const res = arr1.map(e =>
({...e, favorite: arr2.includes(e.id)})
);
console.log(res);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句