Remove duplicate elements from an array in Javascript

I have an array with email and id, so I want to remove duplicate elements that have similar identifiers.

Example:

var newarray=[ { Email:" test1@gmail.com ", ID:"A" }, { Email:" test2@gmail.com ", ID:"B" }, { Email:" test3@gmail.com ", ID:"A" }, { Email:" test4@gmail.com ", ID:"C" }, { Email:" test4@gmail.com ", ID:"C" } ]; 

Now I need to remove duplicate elements that have an identifier. In this, I expect the final array to be

 var FinalArray=[ { Email:" test1@gmail.com ", ID:"A" }, { Email:" test2@gmail.com ", ID:"B" }, { Email:" test5@gmail.com ", ID:"C" } ]; 
+5
source share
4 answers

Use Array.prototype.filter to filter items and to check for duplicates, use the temp array

 var newarray = [{ Email: " test1@gmail.com ", ID: "A" }, { Email: " test2@gmail.com ", ID: "B" }, { Email: " test3@gmail.com ", ID: "A" }, { Email: " test4@gmail.com ", ID: "C" }, { Email: " test5@gmail.com ", ID: "C" }]; // Array to keep track of duplicates var dups = []; var arr = newarray.filter(function(el) { // If it is not a duplicate, return true if (dups.indexOf(el.ID) == -1) { dups.push(el.ID); return true; } return false; }); console.log(arr); 
+5
source

You can filter it using a hash table.

 var newarray = [{ Email: " test1@gmail.com ", ID: "A" }, { Email: " test2@gmail.com ", ID: "B" }, { Email: " test3@gmail.com ", ID: "A" }, { Email: " test4@gmail.com ", ID: "C" }, { Email: " test5@gmail.com ", ID: "C" }], filtered = newarray.filter(function (a) { if (!this[a.ID]) { this[a.ID] = true; return true; } }, Object.create(null)); console.log(filtered); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

ES6 with Set

 var newarray = [{ Email: " test1@gmail.com ", ID: "A" }, { Email: " test2@gmail.com ", ID: "B" }, { Email: " test3@gmail.com ", ID: "A" }, { Email: " test4@gmail.com ", ID: "C" }, { Email: " test5@gmail.com ", ID: "C" }], filtered = newarray.filter((s => a => !s.has(a.ID) && s.add(a.ID))(new Set)); console.log(filtered); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
+3
source

If you can use Javascript libraries such as underscores or lodash, I recommend taking a look at the _.uniq function in your libraries. From lodash:

 _.uniq(array, [isSorted=false], [callback=_.identity], [thisArg]) 

Here you should use as below

 var non_duplidated_data = _.uniq(newarray, 'ID'); 
+2
source

Another solution using Array.prototype.reduce and a hash table - see the demo below:

 var newarray=[ { Email:" test1@gmail.com ", ID:"A" }, { Email:" test2@gmail.com ", ID:"B" }, { Email:" test3@gmail.com ", ID:"A" }, { Email:" test4@gmail.com ", ID:"C" }, { Email:" test5@gmail.com ", ID:"C" } ]; var result = newarray.reduce(function(hash){ return function(prev,curr){ !hash[curr.ID] && (hash[curr.ID]=prev.push(curr)); return prev; }; }(Object.create(null)),[]); console.log(result); 
 .as-console-wrapper{top:0;max-height:100%!important;} 
+1
source

Source: https://habr.com/ru/post/1259451/


All Articles