Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

两个数组相同的去重

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

如果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}”去重了

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...