js如何匹配两个对象数组!不要嵌套循环

发布于 2022-09-02 00:53:12 字数 283 浏览 6 评论 0

var arr1 = [{id:1,name:"a",y:true},{id:2,name:"b",y:true},{id:3,name:"c",y:true},{id:4,name:"d",y:true},{id:5,name:"e",y:true}];
var arr2 = [{id:1,name:"a",y:true},{id:2,name:"b",y:true}];

怎么用arr2匹配arr1,如果arr1中某项的id等于arr2某项的id,就给arr1中相应相等的项的y改为false;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

千笙结 2022-09-09 00:53:12

没有嵌套 :)

var arr1 = [{id:1,name:"a",y:true},{id:2,name:"b",y:true},{id:3,name:"c",y:true},{id:4,name:"d",y:true},{id:5,name:"e",y:true}];
var arr2 = [{id:1,name:"a",y:true},{id:2,name:"b",y:true},{id:4,name:"b",y:true}];

var x = [];

arr1.forEach(v => x[v.id] = v);

arr2.forEach(v => x[v.id] && (x[v.id].y = false));

x = null;

console.log(arr1); // 结果
九歌凝 2022-09-09 00:53:12

利用Array的sort方法及concat方法引用复制的特点来实现查找

concat方法引用复制

Object references (and not the actual object): concat copies object references into the new array. Both the original and new array refer to the same object. That is, if a referenced object is modified, the changes are visible to both the new and original arrays.
Strings and numbers (not String and Number objects): concat copies the values of strings and numbers into the new array.

sort方法
如果2者相同,那么不交换彼此的位置,也就是说如果arr1中的某项a和arr2中的某项b相同,那么排序结束后,
a会一一直保持在b元素的前面

var arr1 = [{id:1,name:"a",y:true},{id:2,name:"b",y:true},{id:3,name:"c",y:true},{id:5,name:"d",y:true},{id:7,name:"e",y:true}];
var arr2 = [{id:1,name:"a",y:true},{id:2,name:"b",y:true},{id:7,name:"b",y:true},{id:5,name:"b",y:true}];

arr1.concat(arr2).sort(function(a,b){
    if(a.id== b.id){
        a.y=false;
    }
    return a.id-b.id;

});
console.log(arr1);
等风来 2022-09-09 00:53:12

使用map 函数咯

嘿哥们儿 2022-09-09 00:53:12
const arr1 = [{id:1,name:"a",y:true}
               ,{id:2,name:"b",y:true}
               ,{id:3,name:"c",y:true}
               ,{id:4,name:"d",y:true}
               ,{id:5,name:"e",y:true}
            ];
const arr2 = [{id:1,name:"a",y:true},{id:2,name:"b",y:true}];

let tmap = {}

arr2.forEach(ele=>tmap[ele.id]=true)

let arr1X = arr1.map(ele=> Object.assign({},ele,!tmap[ele.id]?{}:{y:false}) )

console.log(arr1X)
  • 注意的是该方法由于使用了Object.assign导致效率较差。

改成这样会比较快


arr2.forEach(ele=>tmap[ele.id]=true)

arr1.forEach(ele=> {
    if(!tmap[ele.id]){
        ele.y = false
    }
})

http://jsperf.com/resolve2array1010000004329153/5

囚你心 2022-09-09 00:53:12
var arr1 = [{id: 1, name: "a", y: true}, {id: 2, name: "b", y: true}, {id: 3, name: "c", y: true}, {id: 4, name: "d", y: true}, {id: 5, name: "e", y: true}];
var arr2 = [{id: 1, name: "a", y: true}, {id: 2, name: "b", y: true}];
var val = JSON.stringify(arr2);
arr1.forEach(function (item) {
     if (new RegExp('"id":' + item.id, 'g').test(val)) {
          item.y = false;
     }
});
逆蝶 2022-09-09 00:53:12

将数组arr1转为对象(其实是个map),然后直接查找就行了。

var mapOfArr1 = {};
arr1.forEach(function(e){
    mapOfArr1[e.id] = e;
});
arr2.forEach(function(e){
    mapOfArr1[e.id] && (mapOfArr1[e.id].y = false);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文