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

148
Visualizações
How to get same value from array of objects using javaScript?

I have two arrays, my aim is to filter the second array arr2 by the first array arr1, and get only matched values where prid is the same as arr2 id, results should be let filtred = [ {name:'kiwi', color:'red', id:12123},{name:'kiwi', color:'red', id:12123}], how i can ichieve this using filter?

let arr1 = [
  {prid:12123},
  {prid:12124}
]

let arr2 = [
  {name:'kiwi', color:'red', id:12123},
  {name:'kiwi2', color:'blue',id:12129},
  {name:'kiwi3', color:'green',id:12124}
]

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Please note: According to your requirement your expected output is not correct:

You can try using Array.prototype.some() inside Array.prototype.filter() by matching the respective property value (prid of arr1 with id of arr2).

let arr1 = [
  {prid:12123},
  {prid:12124}
]

let arr2 = [
  {name:'kiwi', color:'red', id:12123},
  {name:'kiwi2', color:'blue',id:12129},
  {name:'kiwi3', color:'green',id:12124}
]

let filtred = arr2.filter(i => arr1.some(j => j.prid == i.id))
console.log(filtred);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.prototype.reduce which allows to create another array with different structure

let arr1 = [
  {prid:12123},
  {prid:12124}
]

let arr2 = [
  {name:'kiwi', color:'red', id:12123},
  {name:'kiwi2', color:'blue',id:12129},
  {name:'kiwi3', color:'green',id:12124}
]

let results = arr2.reduce((accumulator, currentItem) =>{
  let itemExists = arr1.findIndex(item => item.prid === currentItem.id);
  return itemExists !== -1? accumulator.concat(currentItem): accumulator;
}, []);

console.log(results);

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