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

273
Views
How do I extract an object from a json file based on a nested value (using JavaScript)
    const data = [
   {0: {id: "1",cat:{string:[{color:"yellow",weight:"10"},{color:"orange",Weight:"10"}]}},
   {1: {id: "1",cat:{string:[{color:"blue",weight:"10"},{color:"orange",Weight:"10"}]}},
   {2: {id: "1",cat:{string:[{color:"white",weight:"10"},{color:"orange",Weight:"10"}]}},
   {3: {id: "1",cat:{string:[{color:"blue",weight:"10"},{color:"orange",Weight:"10"}]}},
]

Filter by Color: "Yellow"

Desired Output: [{0: {id: 1, country: "SA", address: "IOXX",cat:[{color: "yellow",weight: "10"}]}}]

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

0

You can map over your data array and check if each object passes your conditions or not.

const data = [
 {0: {id: 1, country: "SA", address: "IOXX",cat:[{color: "yellow",weight: "10"}]}},
 {1: {id:2, country: "SAP", name: "N", address: "IOP",cat:[{color: "blue",weight: "10"}]}},
 {2: {id:3, country: "S", name: "NO", address: "I",cat:[{color: "blue",weight: "10"}]}},
 {3: {id:4, country: "SXX", name: "NOI", address: "INDIA",cat:[{color: "blue",weight: "12"}]}},
]

const filterByColor = function (color) {
  const filteredCats = []
  data.forEach((item, index) => {
    const hasCorrectColor = item[index].cat.every((cat) => cat.color === color)
    if (hasCorrectColor) {
       filteredCats.push(item)
    }
      
  })
  return filteredCats
}

filterByColor('yellow')
about 4 years ago · Juan Pablo Isaza Report

0

You can use filter() method

const data = [
    { 0: { id: "1", cat: { string: [{ color: "yellow", weight: "10" }, { color: "orange", Weight: "10" }] } } },
    { 1: { id: "1", cat: { string: [{ color: "blue", weight: "10" }, { color: "orange", Weight: "10" }] } } },
    { 2: { id: "1", cat: { string: [{ color: "white", weight: "10" }, { color: "orange", Weight: "10" }] } } },
    { 3: { id: "1", cat: { string: [{ color: "blue", weight: "10" }, { color: "orange", Weight: "10" }] } } }
];

var res = data.filter((el, i) => data[i][i]['cat']['string'][0]['color'] === 'yellow');

console.log(res);

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!