JS 判断数组B是否存在A数组问题？

KenOscar
• 866

``````[
{
"isSelect": 0,
"rowLocked": 0,
"sortNo": 1
},
{
"id": "9a765b3ba453428ca7c9dff20974a173",
"isSelect": 0,
"rowLocked": 0,
"sortNo": 2
},
{
"id": "a6aa8ba1e5ba489598e708afdde0531b",
"isSelect": 0,
"rowLocked": 0,
"sortNo": 3
},
{
"isSelect": 0,
"rowLocked": 0,
"sortNo": 4,
"validity": 1658419200000
},
{
"id": "1813c65bd6cf4b489f8baf0b37d31427",
"isSelect": 1,
"rowLocked": 0,
"sortNo": 5
},
{
"id": "7895f65431fb405cbb463de58034043c",
"isSelect": 0,
"rowLocked": 0,
"sortNo": 6
}
]``````

``````[
{
"isSelect": 0,
"rowLocked": 0,
"sortNo": 1
},

]``````

5 个回答
✓ 已被采纳

1.A先做一个id数组 `const aids= = ['4c8...','9a7','a6a'...]`

{

``4c80da7e5fac4c43adcb7e79dc6feeeb：id为它的那项``

}
2.change方法触发的时候 数组B的长度会变 遍历B拿到id数组 `const bids = Barr.map(i=>i.id)`
3.遍历A 如果id在bids里面则select=1否则select=0

``````aids.forEach(i=>{
bids.includes(i)?hash表[i].select=1:hash表[i].select=0
})``````

``````A.map(itemA => {
return {
...itemA,
isSelect: B.some(itemB => itemB.id === itemA.id) ? 1: 0
}
})``````

• 126

• 54.8k

``````function setMarkBy(origin, reference) {
const idSet = new Set(reference.map(({ id }) => id));
origin.forEach(it => {
it.isSelect = idSet.has(it.id) | 0;
});
return origin;
}

console.log(setMarkBy(a, b));``````

``````function setMarkBy(origin, reference) {
origin.forEach(it => {
it.isSelect = reference.find(({ id }) => it.id === id) ? 1 : 0;
});
return origin;
}``````

• 2

``````const A = [];
const B = [];
const change = () => {
const C = B.map(i => i.id);
A.forEach(i => C.include(i.id) ? ({...i, isSelected : 1}) : ({...i, isSelected : 0}))
}``````

你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进