比如我有源数据:
arr1 = {
0:{id:1,name:"aaa"},
1:{id:2,name:"bbbb"},
2:{id:3,name:"bbbb"},
3:{id:4,name:"bbbb"},
4:{id:5,name:"bbbb"},
5:{id:6,name:"bbbb"},
6:{id:7,name:"bbbb"},
....(很多,2千个左右)
}
我想取出数组arr2中arr1的数据:
arr2=[1,5,3]
如上:我想在arr1中取出id为 1,5,3的数据,
也就是最终结果为:
arr={
1:{id:2,name:"bbbb"},
5:{id:6,name:"bbbb"},
3:{id:4,name:"bbbb"}
}
应该怎么写?
(实际上arr1大约2000余条数据,arr2大约500余条数据,怎样效率更高些)
emmm为啥不让后端多表查询呢
let arr1 = [
{id:1,name:"aaa"},
{id:2,name:"bbbb"},
{id:3,name:"bbbb"},
{id:4,name:"bbbb"},
{id:5,name:"bbbb"},
{id:6,name:"bbbb"},
{id:7,name:"bbbb"},
];
let arr2 = [1, 5, 3];
console.log(arr1.filter(v => arr2.includes(v.id)))