So what I have is two arrays of objects, one with people and the other with jobs like this:
[
{
id: 1,
name: 'Daniel',
job: 146
},
{
id: 2,
name: 'Jose',
job: 163
}
]
and:
[
{
id: 146,
name: 'Developer'
},
{
id: 163,
name: 'DevOps'
}
]
I want to obtain a single array of people where the job id is replaced with the whole job object itself, like this:
[
{
id: 1,
name: 'Daniel',
job: {
id: 146,
name: 'Developer'
},
},
{
id: 2,
name: 'Jose',
job: {
id: 163,
name: 'DevOps'
}
}
]
Maybe lodash is not the best approach, but I have no other idea. Help if you can and thank you.
Using Array.map and Array.find
var persons=[
{
id: 1,
name: 'Daniel',
job: 146
},
{
id: 2,
name: 'Jose',
job: 163
}
];
var jobs=[
{
id: 146,
name: 'Developer'
},
{
id: 163,
name: 'DevOps'
}
];
var retVal=persons.map(p=>{
return {...p,job:jobs.find(j=> j.id==p.job)};
})
console.log(retVal)