Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!