0

I am trying to delete some objects that I have duplicated in my array.

I have an array like this.

[{ box: 1, color: 'red', size: 10 },
 { box: 2, color: 'yellow', size: 7},
 { box: 2, color: 'red', size: 10 },
 { box: 1, color: 'red', size: 10 },
 { box: 3, color: 'red', size: 10 }]

So I need to delete the object or objects that are duplicated in all properties. In this case only the box: 1 with color: red and size: 10 is duplicated.

So the output should be this.

[{ box: 1, color: 'red', size: 10 },
 { box: 2, color: 'yellow', size: 7},
 { box: 2, color: 'red', size: 10 },
 { box: 3, color: 'red', size: 10 }]

I have been reading about some functions that delete duplicated objects... but I could not solve my problem...

Any help? Thank you in advance

Joey Phillips
  • 1,543
  • 2
  • 14
  • 22
Sergio Cano
  • 690
  • 3
  • 13
  • 36
  • You can use a filter function. Take a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter – Marco Apr 05 '19 at 16:26
  • Please [**search**](/search?q=%5Bjs%5D+remove+duplicate+objects+from+array) before posting. More on searching [here](/help/searching). The [linked question](https://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-objects-in-javascript) is the *top hit*. – T.J. Crowder Apr 05 '19 at 16:31
  • i did find that post, but i could not apply it in my code... maybe i didnt understand it or i am doing something wrong – Sergio Cano Apr 05 '19 at 16:38
  • Here's something simple you can apply to your situation https://jsfiddle.net/wb1kyapo/1 – charlietfl Apr 05 '19 at 16:43
  • @charlietfl Thank you! it works!... now i need understand what the hell you did xD – Sergio Cano Apr 05 '19 at 16:47
  • ok...I tried to add comments to explain it. Try logging that `tmp` object. Might make more sense then if you look at it – charlietfl Apr 05 '19 at 16:49

0 Answers0