How to remove duplicate values from an object array easily?

Asked

Viewed 11,109 times

3

I have the following array:

[  
   {  
      "value":"ETH",
      "label":"ETH"
   },
   {  
      "value":"LTC",
      "label":"LTC"
   },
   {
      "value":"ETH",
      "label":"ETH"
   }
]

As you can see, there are duplicate values. What is the best way to remove them?

I’ve tried to do these two ways, but to no avail:

let values = [  
   {  
      "value":"ETH",
      "label":"ETH"
   },
   {  
      "value":"LTC",
      "label":"LTC"
   },
   {
      "value":"ETH",
      "label":"ETH"
   }
]

console.log(values.filter((elem, index, self) => index === self.indexOf(elem)))

console.log([... new Set(values)])

I don’t understand why the set didn’t work, since it tries to create a single value list.

  • give a look here https://answall.com/questions/16483/remover-elements-repeating-dentro-de-um-vetor-em-javascript

  • 1

    @Weessmith The answer to this question (as it indicates) does not work for an object vector.

2 answers

6


A simple way to remove duplicates is like this:

let values = [  
   {  
      "value":"ETH",
      "label":"ETH"
   },
   {  
      "value":"LTC",
      "label":"LTC"
   },
   {
      "value":"ETH",
      "label":"ETH"
   }
]

values = values.filter(function (a) {
	return !this[JSON.stringify(a)] && (this[JSON.stringify(a)] = true);
}, Object.create(null))

console.log(values)

Explanation:

Basically what it does is iterate over the array objects and add its serialized value to the temporary object created in the 2nd argument as a key, and thus indicate if it has already been added by placing it the value true. Thus, when a duplicate value arrives, the conditional !this[JSON.stringify(a)] of false, not adding it to the filter.

Theoretically, if 2 objects are equal, they have the same value.

Why the set didn’t work?

Set compares the address of objects by default, see an example:

  let set = new Set()
  let a = {}, b = {}, c = {}
  set.add(a)
  set.add(b)
  set.add(b)
  console.log(set.size)
  console.log(set.has(a))
  console.log(set.has(b))
  console.log(set.has(c))

  • The problem with the answer is that it checks only one key (value)... if the other key (label) is different, it removes it the same way.

  • @True dvd. I modified the answer to suit.

  • A doubt.... I was using your solution to remove duplicates in case of my use. Only that a problem arose. I put the code below in a function is called every time the value of the select field is changed. values = values.filter(Function(a) { Return ! this[JSON.stringify(a)] && (this[JSON.stringify(a)] = true); }, Object.create(null)) Only when I select, for example, "A" and then "B" everything goes fine. Duplicate values are removed, but if I go back to "A" the returned array is empty. How do I resolve this so as not to bring the empty array but without the repeated values

  • @Leonardo did not get it right. I think the ideal is to create a question for this problem specifically. If you do, send the link aq q I try to help.

4

var original = [{a:1}, {a:1}, {a:2}, {a:3}, {a:1}, {a:2}, {a:5}];
var reduced = [];

original.forEach((item) => {
    var duplicated  = reduced.findIndex(redItem => {
        return item.a == redItem.a;
    }) > -1;

    if(!duplicated) {
        reduced.push(item);
    }
});

console.log(JSON.stringify(reduced));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.