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

118
Vistas
Extract data from array if IDs match

I am struggling to find out a solution to my problem, but at the moment I cannot come up with the right one.

When I have my two arrays of objects I want to filter based on category IDs and extract the data from the second one into a new array for example :

    const array1 = [
         {id: 1, name: 'Tropical'},
         {id: 2, name: 'Common'}
]
    
    const array2 = [
         {id:1, name: 'Banana', category_id: 1},
         {id:2, name: 'Mango', category_id: 1},
         {id:3, name: 'Apple', category_id: 2},
    ]

And when click happens I detect the first ID and render the new array only with data that matches the ID. Click Tropical New array :

[
 {id:1, name: 'Banana', category_id: 1},
 {id:2, name: 'Mango', category_id: 1},
]

I would be happy if someone give me a hint on how can I tackle this problem. Thanks !

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Use reduce to map over each item in array1 and filter to grab the items of that category_id

const array1 = [{
    id: 1,
    name: 'Tropical'
  },
  {
    id: 2,
    name: 'Common'
  }
]

const array2 = [{
    id: 1,
    name: 'Banana',
    category_id: 1
  },
  {
    id: 2,
    name: 'Mango',
    category_id: 1
  },
  {
    id: 3,
    name: 'Apple',
    category_id: 2
  },
]

const obj = array1.reduce((acc, cur) => {
  acc[cur.name] = array2.filter(v => v.category_id === cur.id)
  return acc
}, {})

console.log(obj)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could do something like filtering array2 and taking all the elements that have Tropical as name in array1.

const array1 = [
     {id: 1, name: 'Tropical'},
     {id: 2, name: 'Common'}
]

const array2 = [
     {id:1, name: 'Banana', category_id: 1},
     {id:2, name: 'Mango', category_id: 1},
     {id:3, name: 'Apple', category_id: 2},
]

// take tropical friuts
let tropicalFriuts = array2.filter(x => x.category_id === array1.filter(y => y.name === 'Tropical')[0].id);
console.log(tropicalFriuts);

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understood your problem you want before find the id, based on the name of the category, and later filter array2 data based on this id.

const array1 = [{
    id: 1,
    name: 'Tropical'
  },
  {
    id: 2,
    name: 'Common'
  }
]

const array2 = [{
    id: 1,
    name: 'Banana',
    category_id: 1
  },
  {
    id: 2,
    name: 'Mango',
    category_id: 1
  },
  {
    id: 3,
    name: 'Apple',
    category_id: 2
  },
]

const id_key = array1.find(item=>item.name === 'Tropical').id;
const result = array2.filter(item=>item.category_id === id_key);
console.log(result);

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