在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ javascript 多維數(shù)組循環(huán)后,重新生成新數(shù)組

javascript 多維數(shù)組循環(huán)后,重新生成新數(shù)組

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

最終想實(shí)現(xiàn):

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}……];

說白了,就是想把job這個數(shù)組里面的元素拉出來,然后重新生成一個數(shù)組。
我用過foreach 和map,但是總發(fā)現(xiàn)差一點(diǎn)。

回答
編輯回答
入她眼

大概是這樣的邏輯
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)

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);
2017年5月4日 07:43
編輯回答
司令

遞歸可以實(shí)現(xiàn)

2018年7月28日 06:07