6744

json data check if same id repeat

Question:

I have a json data there one id has different color_id. So from there I just want to check if same id repeat then just keep first one

Here is my sample JSON

var data= [{ "id": "1", "name": "xxx", "age": "22","color_id": "22" }, { "id": "1", "name": "yyy", "age": "15","color_id": "1" }, { "id": "5", "name": "zzz", "age": "59","color_id": "22" }];

My desired output

var data= [{ "id": "1", "name": "xxx", "age": "22","color_id": "22" }, { "id": "5", "name": "zzz", "age": "59","color_id": "22" }];

I tried reduce but there I found that modify data structure so I am not sure that I will get my desired output or not.

Answer1:

<pre class="snippet-code-js lang-js prettyprint-override">var data = [{ "id": "1", "name": "xxx", "age": "22", "color_id": "22" }, { "id": "1", "name": "yyy", "age": "15", "color_id": "1" }, { "id": "5", "name": "zzz", "age": "59", "color_id": "22" } ]; let map = {}; let uniqueEntries = data.filter((el) => map[el.id] ? false : map[el.id] = true); console.log(uniqueEntries )

<strong>Explaination:</strong>

<ol><li>You create a map where we store ids. </li> <li>Then filter the array and everytime we find an entry that is not in the map we add it to the list and return true. If we already have it in the list we return false to discard that entry.</li> </ol>

the last part of the conditional is using the fact that an assignment returns the assigned value.

Answer2:

You can use reduce to create a new array and in this new array use findIndex to check if this new array has an object with same id. If there exist an object with same id then dont push another object with same id

<pre class="snippet-code-js lang-js prettyprint-override">var data = [{ "id": "1", "name": "xxx", "age": "22", "color_id": "22" }, { "id": "1", "name": "yyy", "age": "15", "color_id": "1" }, { "id": "5", "name": "zzz", "age": "59", "color_id": "22" } ]; let m = data.reduce(function(acc, curr) { let findIndex = acc.findIndex(function(item) { return item.id === curr.id }) if (findIndex === -1) { acc.push(curr) } return acc; }, []) console.log(m)

Answer3:

Using Array.reduce and Array.some

<pre class="snippet-code-js lang-js prettyprint-override">const data = [{ id: '1', name: 'xxx', age: '22', color_id: '22', }, { id: '1', name: 'yyy', age: '15', color_id: '1', }, { id: '5', name: 'zzz', age: '59', color_id: '22', }, ]; const reduced = data.reduce((tmp, x) => { if (tmp.some(y => y.id === x.id)) return tmp; return [ ...tmp, x, ]; }, []); console.log(reduced); <hr />

Or Array.filter as it was a good idea from @JGoodgive, but kinda different

<pre class="snippet-code-js lang-js prettyprint-override"> const data = [{ id: '1', name: 'xxx', age: '22', color_id: '22', }, { id: '1', name: 'yyy', age: '15', color_id: '1', }, { id: '5', name: 'zzz', age: '59', color_id: '22', }, ]; const reduced = data.filter((x, xi) => !data.slice(0, xi).some(y => y.id === x.id)); console.log(reduced);

Answer4:

You can use a Set to keep track of the IDs already processed.

<pre class="snippet-code-js lang-js prettyprint-override">const // The data set with non-unique IDs data= [{ "id": "1", "name": "xxx", "age": "22","color_id": "22" }, { "id": "1", "name": "yyy", "age": "15","color_id": "1" }, { "id": "5", "name": "zzz", "age": "59","color_id": "22" }]; function dedupe(items) { // Create a set to keep track of IDs already encountered. const idSet = new Set(); // Filter the items, when an ID isn't in the set add it to the set and return true // so item is in the result array. When the ID is in the set return false so the // item will be dropped. return items.filter(item => { // If the ID is already in the set, drop it from the result. This way only the // first item with an ID is added to the result. if (idSet.has(item.id)) { return false; } // Add the ID to the set, this way we keep track of the IDs already encountered. idSet.add(item.id); // Return true so the item is included in the result array. return true; }); } console.log(dedupe(data));

Recommend

  • Design or Class/Component-hierarchy to be followed for the wizard application
  • Error: Cannot determine the module for class OverlayPortal in --prod
  • Why VBA goes to error handling code when there is no error?
  • finding symmetric difference/unique elements in multiple arrays in javascript
  • Matrix problem Python
  • Grouping vars in function
  • Modify a Google App Engine entity id?
  • Click button with javascript
  • How can I see a list of all files that are different between two Hg repositories?
  • Receiver has no segue with identifier“***”
  • What Makes These Two Array Adds Different?
  • Updating Dojo provide
  • Detect when Facebook like button is clicked
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • How do I change content of ComboFieldEditor?
  • Spring security and special characters
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • Weird JavaScript statement, what does it mean?
  • Do I've to free mysql result after storing it?
  • How to pass list parameters for each object using Spring MVC?
  • Comma separated Values
  • retrieve vertices with no linked edge in arangodb
  • SQL merge duplicate rows and join values that are different
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Error creating VM instance in Google Compute Engine
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • Linking SubReports Without LinkChild/LinkMaster
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Python/Django TangoWithDjango Models and Databases