Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
How can remove the node from JSON if values are same in JSON

We want to remove duplicate node from JSON. This node will be removed if trait_type values are same

Here is JSON

[
    {
      "trait_type": "Background",
      "value": "Yellow"
    },
    {
      "trait_type": "A",
      "value": "None"
    },
    {
      "trait_type": "B",
      "value": "Male Body Grey Color"
    },
    
    {
      "trait_type": "A",
      "value": "Outfit"
    }

  ]

Final JSON should like this.

 [
    {
      "trait_type": "Background",
      "value": "Yellow"
    },
    {
      "trait_type": "A",
      "value": "None"
    },
    {
      "trait_type": "B",
      "value": "Male Body Grey Color"
    }

  ]

Please help

Thanks

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

So I guess your json is a javascript array, else use JSON.parse to convert it.

So what you want is to delete double value in the array and to be faire there is a lot to do it, I personnaly use a Set to do that but to be "beginner friendly" I will use a big temporary object to store value and retrive them by a reference

const data = [
    {
      "trait_type": "Background",
      "value": "Yellow"
    },
    {
      "trait_type": "A",
      "value": "None"
    }]

const tmpObject = {};

data.forEach((d) => {
   if (!tmpObject[d?.trait_type]) { 
     tmpObject[d.trait_type] = d; // we only push data indide the object if the keys does not exist and the keys is the value you want to be unique so once you have a value matching we will not add the next data (with same trait_type) inside the object
   }
});

// now build the new array is like

theArrayYouWant = [];
Object.keys(tmpObject).forEach((d) => {
  theArrayYouWant.push(tmpObject[d]);
});

about 4 years ago · Juan Pablo Isaza Report

0

This pure function should do:

function filterUnique(data){
  const unique = {};
  
  data.forEach(el => {
    unique[el.trait_type] = el;
  });
  
  return Object.values(unique);
}

//
const filteredJSON = filterUnique(json);
about 4 years ago · Juan Pablo Isaza Report

0

try this

 var noDuplicatesArr =  origArr.filter((v,i,a)=>a.findIndex(v2=>(v2.trait_type===v.trait_type))===i);

 console.log(noDuplicatesArr);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!