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

120
Vistas
how do I match the data and return same data of first array

I have two array

arr1 =[{name:'Net Banking', id:'NetBanking'},{name:'Debit Card', id:'DebitCard'},{name:'Credit Card', id:'CreditCard'}]

and

arr2=['DebitCard','NetBanking']

and I want to expected result as of arr1

arr1=[{name:'Debit Card', id:'DebitCard'}, {name:'Net Banking', id:'NetBanking'}]

currently I am using -

arr1.filter(data=>arr2.includes(data.id))

and I am getting return as

arr1 =[{name:'Net Banking', id:'NetBanking'},{name:'Debit Card', id:'DebitCard'}]

means match the element of arr2 with arr1 and return as arr1 with same order of arr2

or how can I change the index based on arr2 of arr1

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

0

You can use Array.map() along with Array.find() to get the desired input.

Working Demo :

const arr1 =[{name:'Net Banking', id:'NetBanking'},{name:'Debit Card', id:'DebitCard'},{name:'Credit Card', id:'CreditCard'}];

const arr2=['DebitCard','NetBanking'];

const resArray = arr2.map((item) => arr1.find((obj) => obj.id === item));

console.log(resArray);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can loop through arr2 and for each item find the related object with the same id in array arr1

You can rely on

  1. Array.prototype.find
  2. Array.prototype.reduce

let arr1 =[{name:'Net Banking', id:'NetBanking'},{name:'Debit Card', id:'DebitCard'},{name:'Credit Card', id:'CreditCard'}];

let arr2=['DebitCard','NetBanking'];

const result = arr2.reduce((accumulator, current) => {
   /*
   * For the current item of the loop search for item with
   * object.id === current
   */
   return accumulator.concat(arr1.find(item => item.id === current));
},[]);

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