[英]Javascript remove duplicated object from array
i'm having trouble to remove duplicated object from my array我无法从数组中删除重复的对象
example:例子:
var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}];
in this example i have 3 objects, and i want to remove the object that have the duplicated place在这个例子中,我有 3 个对象,我想删除具有重复位置的对象
Just in case someone wonders: underscore.js
solution:以防万一有人想知道:
underscore.js
解决方案:
var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}];
_.uniq(list, function(item, key, a) {
return item.place;
})
A simple one:一个简单的:
var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}]; list.forEach(function(i) { var duplicates = list.filter(function(j) { return j !== i && j.place == i.place; }); duplicates.forEach(function(d) { list.splice(list.indexOf(d), 1); }); }); // list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}]; document.write(JSON.stringify(list));
Try this.尝试这个。
var result = {};
for (i = 0, n = arr.length; i < n; i++) {
var item = arr[i];
result[ item.place + " - " + item.name ] = item;
}
Loop the result again, and recreate the array.再次循环结果,并重新创建数组。
i = 0;
for(var item in result) {
clearnArr[i++] = result[item];
}
Create a object
to store the items by their place
value, as the new item with the same key will overwrite the old one, this will easily remove all dulplicates.创建一个
object
以按place
值存储项目,因为具有相同键的新项目将覆盖旧项目,这将轻松删除所有重复项。
var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}]; var removeDuplicate = function(list) { var keyStore = {}; var output = []; // If you want to creata totally new one from old, use // list = JSON.parse(JSON.stringify(list)); // The above commented out code will create a copy of list, so the items in output will not affect the original ones. list.forEach(function(item) { // new one overwrites old one. keyStore[item.place] = item; }); var key; for (key in keyStore) { output.push(keyStore[key]); } return output; }; console.log(removeDuplicate(list));
As you added:正如您所添加的:
i want to remove just one, dont matter wich one
我只想删除一个,不管是哪一个
If you want to remove duplicated items and keep only the first occcurence of particular place , you can simply use a simple loop to re-create a new array from the input:如果您想删除重复项并仅保留特定地点的第一次出现,您可以简单地使用一个简单的循环从输入重新创建一个新数组:
var list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}];
var uniqPlace = function(array){
var result = [];
array.forEach(function(el){
if (result.filter(function(n){ return n.place === el.place }).length==0){
result.push(el);
}
})
return result;
}
Output:输出:
uniqPlace(list);
[{"place":"AAA","name":"Me"},{"place":"BBB","name":"You"}]
[{"place":"AAA","name":"Me"},{"place":"BBB","name":"You"}]
3 way to remove duplicate objects from array从数组中删除重复对象的 3 种方法
let list = [{place:"AAA",name:"Me"}, {place:"BBB",name:"You"}, {place:"AAA",name:"Him"}]; let output1 = Array.from(new Set(list.map(list=>list.place))).map(place=>{ return { place: place, name: list.find(a=>a.place===place).name } }) console.log('------------------------1st way') console.log(output1) let output2 = list.reduce((accumulator, element) => { if (!accumulator.find(el => el['place'] === element['place'])) { accumulator.push(element); } return accumulator; },[]); console.log('------------------------2nd way') console.log(output2) const output3 = []; const map = new Map(); for (const object of list) { if(!map.has(object.place)){ map.set(object.place, true); output3.push({ place: object.place, name: object.name }); } } console.log('------------------------3rd way') console.log(output3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.