0

I am Looking For a way to remove Duplicates in a JSON Object with java script

var data = [ { 'id' : '1', 'name' : 'xyz', 'color' : 'red',
}, { 'id' : '2', 'name' : 'abc', 'color' : 'blue',
}, { 'id' : '3', 'name' : 'def', 'color' : 'black',
}, { 'id' : '1', 'name' : 'xyz', 'color' : 'black',
}, { 'id' : '4', 'name' : 'hij', 'color' : 'blue',
}, { 'id' : '2', 'name' : 'abc', 'color' : 'blue',
}, { 'id' : '5', 'name' : 'klm', 'color' : 'yellow',
}, { 'id' : '3', 'name' : 'def', 'color' : 'red',
}, ];

For example i want to remove duplicate id based on the color selected giving priority to red, Black, Blue, Yellow in that order. so if there are two duplicates and person selected red and yellow, red should be kept.

Let me know if you need more clarification.

Thank You Het

  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder May 25 '20 at 15:10
  • Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Please [**search**](/search?q=%5Bjs%5D+array+remove+duplicate) before posting. More about searching [here](/help/searching). – T.J. Crowder May 25 '20 at 15:11
  • Also we're here to help you with your code, not to write code for you. If you want something, then you have to try it yourself first, and show what you've done. If you're stuck we can help. – Jeremy Thille May 25 '20 at 15:12

0 Answers0