Last active
January 10, 2023 01:43
-
-
Save AlonsoK28/426196c855adf0f5ca2c9b93a4c13d7e to your computer and use it in GitHub Desktop.
search into object array using an id as relation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const example = [ | |
{ | |
name: 'Miles Toms', | |
age: 33, | |
city: 1, | |
office: 1, | |
job: 1, | |
lenguage: 1 | |
}, | |
{ | |
name: 'Jhones Miller', | |
age: 20, | |
city: 3, | |
office: 1, | |
job: 2, | |
lenguage: 3 | |
}, | |
{ | |
name: 'Miller Mark', | |
age: 11, | |
city: 4, | |
office: 2, | |
job: 3, | |
lenguage: 2 | |
}, | |
{ | |
name: 'Moe Jhonson', | |
age: 30, | |
city: 4, | |
office: 1, | |
job: 3, | |
lenguage: 4 | |
}, | |
{ | |
name: 'Miller Thomas', | |
age: 21, | |
city: 5, | |
office: 3, | |
job: 3, | |
lenguage: 1 | |
}, | |
{ | |
name: 'Anna Jhones', | |
age: 11, | |
city: 6, | |
office: 4, | |
job: 1, | |
lenguage: 1 | |
}, | |
{ | |
name: 'Rin Miller', | |
age: 45, | |
city: 8, | |
office: 1, | |
job: 2, | |
lenguage: 6 | |
}, | |
{ | |
name: 'Drake Miles', | |
age: 40, | |
city: 8, | |
office: 1, | |
job: 3, | |
lenguage: 6 | |
}, | |
{ | |
name: 'Tim Miller', | |
age: 31, | |
city: 2, | |
office: 3, | |
job: 1, | |
lenguage: 5 | |
} | |
]; | |
const offices = [ | |
{ | |
name: 'Corp', | |
id: 1 | |
}, | |
{ | |
name: 'External 1', | |
id: 2 | |
}, | |
{ | |
name: 'Internal 1', | |
id: 3 | |
}, | |
{ | |
name: 'Internal 2', | |
id: 4 | |
}, | |
{ | |
name: 'Internal 3', | |
id: 5 | |
}, | |
{ | |
name: 'Internal 4', | |
id: 6 | |
} | |
]; | |
const languages = [ | |
{ | |
name: 'Angular', | |
id: 1 | |
}, | |
{ | |
name: 'Typescript', | |
id: 2 | |
}, | |
{ | |
name: 'Java', | |
id: 3 | |
}, | |
{ | |
name: 'C++', | |
id: 4 | |
}, | |
{ | |
name: '.NET', | |
id: 5 | |
}, | |
{ | |
name: 'PHP', | |
id: 6 | |
} | |
] | |
const jobs = [ | |
{ | |
name: 'Desing', | |
id: 1 | |
}, | |
{ | |
name: 'Code', | |
id: 2 | |
}, | |
{ | |
name: 'Arquitect', | |
id: 3 | |
}, | |
{ | |
name: 'Programming', | |
id: 4 | |
} | |
] | |
const cities = [ | |
{ | |
name: 'Brazil', | |
id: 1 | |
}, | |
{ | |
name: 'Argentina', | |
id: 2 | |
}, | |
{ | |
name: 'Mexico', | |
id: 3 | |
}, | |
{ | |
name: 'USA', | |
id: 4 | |
}, | |
{ | |
name: 'Tokyo', | |
id: 5 | |
}, | |
{ | |
name: 'Russia', | |
id: 6 | |
}, | |
{ | |
name: 'Italy', | |
id: 7 | |
}, | |
{ | |
name: 'China', | |
id: 8 | |
}, | |
] | |
const res = example.map((currentValue, index, array) => { | |
const currentOfficeId = currentValue.office; | |
const currentJobId = currentValue.job; | |
const currentCityId = currentValue.city; | |
const currentLenguajeId = currentValue.lenguage; | |
const officeDesc = offices.find(el => el.id === currentOfficeId); | |
const jobDesc = jobs.find(el => el.id === currentJobId); | |
const cityDesc = cities.find(el => el.id === currentCityId); | |
const languageDesc = languages.find(el => el.id === currentLenguajeId); | |
return { | |
...currentValue, | |
office_desc: officeDesc.name, | |
job_desc: jobDesc.name, | |
city_desc: cityDesc.name, | |
lenguage_desc: languageDesc.name | |
}; | |
}) | |
console.log({ res }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment