2

I am working on an angular app. I have an array "*identify duplicate" which is something like this.

[
  {
   Name: "Jack",
   Id: "1"
  },
  {
    Name: "Rose",
    Id: "2"
  },
  {
    Name: "Jack",
    Id: "4"
  },
  {
    Name: "Jack",
    Id: "4"
  }
]

If I get same name and Id I want to remove both. Like in above example in last two indexes name and Id are same "jack" But I don't want to delete any data if name is name and id is different. Like in above example I want to keep Jack with Id "1" in my array but want to delete last two "Jack" with Id "4". How can I do that?

Laode Muhammad Al Fatih
  • 3,994
  • 1
  • 18
  • 32
  • 5
    Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – Neeko Jun 23 '20 at 09:21

4 Answers4

3

You can do this with several methods like filter,reduce,map etc... I create two example below.

First one is simple forEach method. In a loop filter if there is same Name element then push to new array

In second reduce method again filter element if there is same Name element then push to new array.

var arr=[{Name: "Jack",Id: "1"},{Name: "Rose",Id: "2"},{Name: "Jack", Id: "4"},{Name: "Jack", Id: "4"}];

//simple foreach method
var result=[];
arr.forEach(el=>{
  if(result.filter(x=>x.Name==el.Name).length==0){result.push(el);}
});
console.log(result);


//reduce method
var result2=arr.reduce((r, { Name, Id }) => {
    var temp = r.find(o => Name === o.Name);
    if (!temp) { r.push(temp = { Name, Id });}
    return r;
}, []);
console.log(result2);
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54
  • could you please provide explanation for this? –  Jun 23 '20 at 09:40
  • I wrote some explaination about two methods in example snipped @zaib – mr. pc_coder Jun 23 '20 at 09:44
  • I have confusion about Id. How it is deleting element with same name and Id though your check is only of Name? In case suppose I add another element with same name and different Id will it get add? –  Jun 23 '20 at 09:47
  • Yes in loop take first element and control if there is in result with this name if yes skip if not then push to result array then go to another element do same process until loop finishes – mr. pc_coder Jun 23 '20 at 09:50
  • I tried it, It is working fine but just a query if I have a same data at two indexes like {Name: "Zaib", Id: "10"},{Name: "Zaib", Id: "10"}, . It is automatically keeping one {Name: "Zaib", Id: "10"}, and descarding another. How? Though it is a desired result for me. Just want to know how it is working –  Jun 23 '20 at 10:26
  • result array is empt as inital. This array fill new one if there is no element inside result array whose name equals item will be added. In loop first zaib added then when try to second one result array says I have already Zaib skip it – mr. pc_coder Jun 23 '20 at 10:30
1

Using Map()

var arr = [{Name:"Jack",Id:"1"},{Name:"Rose",Id:"2"},{Name:"Jack",Id:"4"},{Name:"Jack",Id:"4"}]

let res = [...arr.reduce((a, i) => a.has(i.Name) ? a : a.set(i.Name, i), new Map).values()]

console.log(res)
User863
  • 19,346
  • 2
  • 17
  • 41
1

Try this:

var a = [
{
 "Name": "Jack",
 "Id": "1"
},
{
  "Name": "Jack",
  "Id": "4"
},
{
  "Name": "Jack",
  "Id": "4"
}
]    
var jsonObject = a.map(JSON.stringify); 
console.log(jsonObject); 
var uniqueSet = new Set(jsonObject); 
var uniqueArray = Array.from(uniqueSet).map(JSON.parse); 
console.log(uniqueArray);
prateek3636
  • 155
  • 1
  • 7
  • Better not using `JSON.parse` as a direct callback, even if it's tempting. `JSON.parse` accepts 3 parameters, `map` provides 3 parameters, but they aren't obviously meaningful – Christian Vincenzo Traina Jun 23 '20 at 09:42
1

If you can use Javascript libraries such as underscore or lodash, I recommend having a look at _.uniq function in their libraries. From lodash

var arr=[ { Name: "Jack", Id: "1" }, { Name: "Rose", Id: "2" }, { Name: "Jack", Id: "4" }, { Name: "Jack", Id: "4" } ]; var updated_data = _.uniq(arr, 'Name');