javascript - 两个数组相同的去重
高洛峰
高洛峰 2017-04-11 12:27:06
[JavaScript讨论组]

两个数组,把其中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"
                    }
                  ]
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(5)
ringa_lee

如果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]);
    }
}
PHPz
/* 只是 单纯 对象 滴 ID 不重复的 话 */
var a = [];
              a = b1.concat(a1).filter(function(o){
                  return a.indexOf(o.ID) === -1 && a.push(o.ID);
              });
              console.log(a);
PHP中文网

伪代码:

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)
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号