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

126
Views
How to match the name by the id from multiple arrays in React

My React application receives an array consisting of one array which holds the ids of two others, and two arrays which contain the id and the name. I want to create one array, which holds the names which fit to the id from array one. Written in an censored form it looks like this:

There is array A, which consists of two ids and the own id (AAAAA_id).

A
{
"AAAAAA_id": "AA",
"BBBBBB_id": "BB",
"CCCCCC_id": "CC"
}

Than there is Array B which consists of BBBBBB_id and the name.

B
{
"BBBBBB_id": "BB",
"BBBBBB_name": "BName",
}

Furthermore there is Array C which consists of CCCCCC_id and the name

C
{
"CCCCCC_id": "CC",
"CCCCCC_name": "CName",
}

How can I achieve an new Array D which consists of

D
{
"AAAAAA_id": "AA",
"BBBBBB_id": "BB",
"BBBBBB_name": "BName",
"CCCCCC_id": "CC"
"CCCCCC_name": "CName",
}

Id did start with a combination of map and filter,

const D = A.map(x => {

But I don't know how to progress from here on to handle multiple arrays.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You can use find

const D = A.map(a => ({
    ...a,
    BBBBBB_name: B.find(b => b.BBBBBB_id == a.BBBBBB_id)?.BBBBBB_name,
    CCCCCC_name: C.find(c => c.CCCCCC_id == a.CCCCCC_id)?.CCCCCC_name,
}))

or better performance solution

const MAP_ID_NAME_B = B.reduce((acc, cur) => {
    acc[cur.BBBBBB_id] = cur.BBBBBB_name
    return acc;
}, {})

const MAP_ID_NAME_C = C.reduce((acc, cur) => {
    acc[cur.CCCCCC_id] = cur.CCCCCC_name
    return acc;
}, {})

const D = A.map(a => ({
    ...a,
    BBBBBB_name: MAP_ID_NAME_B[a.BBBBBB_id],
    CCCCCC_name: MAP_ID_NAME_C[a.CCCCCC_id]
}));
about 4 years ago · Santiago Gelvez 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!