Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda