我正在努力使以下工作正常进行。
我有一个值数组:
const active = ['1', '3', '5']
我想在将此列表与对象数组(关键用户标识)进行比较时:
[{
userid: '1',
username: 'peter'
}, {
userid: '2',
username: 'steve'
}, {
userid: '3',
username: 'ted'
}]
然后产生一个包含活动列的对象更新数组:
data = [{
userid: '1',
username: 'peter',
active: true
}, {
userid: '2',
username: 'steve',
active: false
}, {
userid: '3',
username: 'ted',
active: true
}]
以下是我尝试过的尝试(对不起,很遗憾)...
Object.keys(data).map((key) => {
key.filter((userid) => data.indexOf(data.userid) => active)
})
我也看到了此错误消息,因此对象数组需要可扩展:
js map Error: An error occurred while selecting the store state: Cannot add property isApproved, object is not extensible.
const active = ['1', '3', '5']
const input = [{
userid: '1',
username: 'peter'
}, {
userid: '2',
username: 'steve'
}, {
userid: '3',
username: 'ted'
}];
const res = input.map(x => {
const flag = active.some(y => y === x.userid)
return {
userid: x.userid,
username: x.username,
active: flag
}
})
console.log(res)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句