Regenerate the new array after the javascript multidimensional array loop

var arr= [{id:1,name:1,job:[{a:1,a:2}]},{id:2,name:2,job:[a:3,a:4]}];

finally want to realize:

arr=[{id:1,name:1,a:1},{id:1,name:1,a:2},{id:2,name:2,a:3},{id:2,name:2,a:4}];
To put it bluntly, I just want to pull out the elements in the job array and generate a new array.
I"ve used foreach and map, but I always find it close.

Apr.19,2021

it looks like this logic
ES6

var arr = [{id: 1, name: 1, job: [1, 2]}, {id: 2, name: 2, job: [3, 4]}];
const result = [].concat(...arr.map(({id, name, job}) => (job.map(a => ({id, name, a})))));
console.log(result)
< hr >

ES5

var arr = [{id: 1, name: 1, job: [1, 2]}, {id: 2, name: 2, job: [3, 4]}];
var result = [];
arr.forEach(function (item) {
  item.job.forEach(function (a) {
    result.push({
      id: item.id,
      name: item.name,
      a: a
    })
  })
});
console.log(result);

Recursion can be implemented


1. Like this?

var arr= [{id:1,name:1,job:[{a:1,b:2}]},{id:2,name:2,job:[{a:3,b:4}]}];
arr.reduce((a,b)=>{
    var job = b.job[0]
    delete b.job
    a.push({...b, ...job})
    return a;
},[])
//[{id:1, name:1,{a:1,b:2},{id:2, name:2, a:3, b:4}];

2. Like this?

var arr= [{id:1,name:1,job:[{a:1,b:2}]},{id:2,name:2,job:[{a:3,b:4}]}];
arr.reduce((a,b)=>{
    
    var job = b.job[0]
    delete b.job
    Object.keys(job).forEach(key=>{
        var newObj = {...b};
        newObj[key] = job[key]
        a.push(newObj)
    })
    return a;
},[])

// [{id:1, name:1, a:1}, {{id:1, name:1, b:2},{id:2, name:2, a:3},{id:2, name:2, b:4}];
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b368ad-2c02b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b368ad-2c02b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?