I currently have two arrays containing objects:
let employees = [
{ name: 'Jason', job_id: '101' },
{ name: 'Sarah', job_id: '102' },
{ name: 'Jack', job_id: '102' }
]
let jobs = [
{ job_id: '101', position: 'Designer' },
{ job_id: '102', position: 'Developer' }
]
Is there a way for me to merge these arrays using vanilla javascript, as shown below:
let employees = [
{ name: 'Jason', job_id: [job_id: '101', position: 'Designer'] },
{ name: 'Sarah', job_id: [job_id: '102', position: 'Developer'] },
{ name: 'Jack', job_id: [job_id: '102', position: 'Developer'] }
]
The code snippet I have now does work correctly, but if possible, I would prefer not to rely on nested loops.
employees.forEach(employee => {
for (let index = 0; index < jobs.length; index++) {
if (employee.job_id == jobs[index].job_id) {
employee.job_id= jobs[index];
}
}
})