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

97
Views
compare 2 arrays and get key of one with value of the other one in React

So in React, I have 2 arrays:

const arr1 = [
{id: 1, name: 'Hello'}
{id: 2, name: 'Dear'}
{id: 3, name: 'World'}
]

const arr2 = ['Hello', 'Dear']

I would like the arr2 values to be the id of the arr1 and end up with this:

const arr2 = [1, 2]

Thank you very much

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Loop through arr2 and find the item with the same name in arr1. Then take the id of that item.

const arr1 = [
{id: 1, name: 'Hello'},
{id: 2, name: 'Dear'},
{id: 3, name: 'World'}
]

let arr2 = ['Hello', 'Dear']

let nameToId = arr1.reduce((result, item) => ({ ...result, [item.name]: item.id}), {}); 


arr2 = arr2.map((name) => nameToId[name]);

console.log(arr2);

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!