Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda