两个数组相同的去重

两个数组,把其中ID相同的项去除,返回一个新的数组

var b1=[
                    {
                      "ID": 1,
                      "Name": "sample strin",
                      "ImgUrl": "sample stng 3"
                    },
                    {
                      "ID": 2,
                      "Name": "sample strin2",
                      "ImgUrl": "sample string 3"
                    },
                    {
                      "ID": 3,
                      "Name": "sample strin",
                      "ImgUrl": "sample stng 3"
                    },
                    {
                      "ID": 4,
                      "Name": "sample strin2",
                      "ImgUrl": "sample string 3"
                    },
                    {
                      "ID": 5,
                      "Name": "sample strin",
                      "ImgUrl": "sample stng 3"
                    },
                    {
                      "ID": 6,
                      "Name": "sample strin2",
                      "ImgUrl": "sample string 3"
                    },
                    {
                      "ID": 8,
                      "Name": "sample strin2",
                      "ImgUrl": "sample string 3"
                    },
                    {
                      "ID": 9,
                      "Name": "sample strin",
                      "ImgUrl": "sample stng 3"
                    },
                    {
                      "ID": 10,
                      "Name": "sample strin2",
                      "ImgUrl": "sample string 3"
                    },
                    {
                      "ID": 11,
                      "Name": "sample strin",
                      "ImgUrl": "sample stng 3"
                    },
                    {
                      "ID": 12,
                      "Name": "sample strin2",
                      "ImgUrl": "sample string 3"
                    }
                  ];

图片描述

var a1= [
                    {
                      "ID": 1,
                      "Name": "sample strin",
                      "ImgUrl": "sample stng 3"
                    },
                    {
                      "ID": 2,
                      "Name": "sample strin2",
                      "ImgUrl": "sample string 3"
                    },
                    {
                      "ID": 3,
                      "Name": "sample strin",
                      "ImgUrl": "sample stng 3"
                    },
                    {
                      "ID": 4,
                      "Name": "sample strin2",
                      "ImgUrl": "sample string 3"
                    },
                    {
                      "ID": 5,
                      "Name": "sample strin",
                      "ImgUrl": "sample stng 3"
                    },
                    {
                      "ID": 6,
                      "Name": "sample strin2",
                      "ImgUrl": "sample string 3"
                    }
                  ]
阅读 9.7k
5 个回答
/* 只是 单纯 对象 滴 ID 不重复的 话 */
var a = [];
              a = b1.concat(a1).filter(function(o){
                  return a.indexOf(o.ID) === -1 && a.push(o.ID);
              });
              console.log(a);

如果id相同的项,其name和imgurl字段也是相同的,那么可以直接合并数组,然后用ES6中的将数组转化为Set结构再转回数组结构就去重了

如果id相同,但是name和imgurl字段不同,那就遍历一项一项处理吧

补充:确实数组每一项是对象,用set不行,但是可以变通啊,将每一项的对象转化为字符串啊,而这个转化也很简单,如下

var a=[{"a":1,"b":2},{"a":1,"b":2},{"a":100,"b":200}];
var aStr=a.map((value)=>JSON.stringify(value));
//console.log(aStr);
var aWithNoRepeat=Array.from(new Set(aStr));
//console.log(aWithNoRepeat);
aWithNoRepeat=aWithNoRepeat.map((value)=>JSON.parse(value));
//console.log(aWithNoRepeat);
//至此已经把重复的对象“{"a":1,"b":2}”去重了

如果数组内部没有重复,只需要把第二个数组元素一个一个取出,然后比对是否在第一个数组中存在,不存在的话就加入第一个数组。当然,如果是排好序的可能会更简单。

var result = [],t ={};
var i;
for(i=0;i<a1.length;i++){
    t[a1[i].ID] = a1[i];
}
for(i=0;i<b1.length;i++){
    t[b1[i].ID] = b1[i];
}
for(i in t){
    if(t.hasOwnProperty(i)){
        result.push(t[i]);
    }
}

伪代码:

function unify(arrays) {

  const temp = {}
  function push(id, e) {
    if (temp.has(id)) temp[id].push(e)
    else temp[id] = [e]
  }
  arrays.loop(array => array.loop(e => push(e.id, e) ))

  temp.filter(e => e.length === 1)
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题