如何在JavaScript中将多个jsons数组合并为一个与子数组交叉的数组?

时间:2018-09-28 19:24:45

标签: javascript arrays json merge let

我有两个json数组:

 1) 
   [
    {
     "userId": 9
    },
    {
      "userId": 14
     }
    ]

 2) 
   [{"role": "1", "group": "3"}, {"role": "1", "group": "2"}] 

我想合并两个数组,如下所示:JavaScript是否可以提供解决方案?

[
 {"userId":9,"role":"1","group":"2"},
 {"userId":14,"role":"1","group":"2"}
 {"userId":9,"role":"1","group":"3"},
 {"userId":14,"role":"1","group":"3"}
] 

我尝试使用let,但是我找不到操纵切换子数组的方法:

let arr1 = [{"userId": 9}, {"userId": 14}];
let arr2 = [{"role": "1", "group": "3"}, {"role": "1", "group": "2"}];

let result = arr1.map(o => Object.assign(o, ...arr2));

console.log(result);
return result;

我在以前的实现中得到的结果是这样的:

[{"userId":9,"role":"1","group":"2"},{"userId":14,"role":"1","group":"2"}] 

但是,我想得到如下结果:

[
 {"userId":9,"role":"1","group":"2"},
 {"userId":14,"role":"1","group":"2"}
 {"userId":9,"role":"1","group":"3"},
 {"userId":14,"role":"1","group":"3"}
]

6 个答案:

答案 0 :(得分:4)

var a = [{
    "userId": 9
  },
  {
    "userId": 14
  }
]

var b = [{
  "role": "1",
  "group": "3"
}, {
  "role": "1",
  "group": "2"
}]

console.log(
  b.map(z=>a.map(x=>({...x, ...z}))).flat()
)

答案 1 :(得分:1)

使用for循环的另一种解决方案

let arr1 = [{ "userId": 9 }, { "userId": 14 }]
let arr2 = [{"role": "1","group": "3"}, {"role": "1","group": "2" }] 
let result = [];
for (let group of arr2) {
  for (let user of arr1) [
    result.push(Object.assign({}, group, user))
  ]
}
console.log(JSON.stringify(result))
//output is: 
// [
//  {"role":"1","group":"3","userId":9},
//  {"role":"1","group":"3","userId":14},
//  {"role":"1","group":"2","userId":9},
//  {"role":"1","group":"2","userId":14}
// ]

Stackblitz example

答案 2 :(得分:1)

要获得预期的结果,请使用下面的选项遍历两个数组

 var x = [
    {
     "userId": 9
    },
    {
      "userId": 14
     }
    ]

 var y = [{"role": "1", "group": "3"}, {"role": "1", "group": "2"}] 
 let result = []
 
y.forEach((v, i) =>{
  x.forEach((y,i) => result.push({...v, ...y}))
})

console.log(result);

codepen-https://codepen.io/nagasai/pen/pxvzOG?editors=1010

答案 3 :(得分:0)

您可以混合使用reducemap将其煮沸成单个对象数组。

let data = [{
    "userId": 9
  },
  {
    "userId": 14
  }
]

let metaData = [{
  "role": "1",
  "group": "3"
}, {
  "role": "1",
  "group": "2"
}];

let dataReducer = data.reduce((acc, curr) => {
	let metadataReducer = metaData.map((val) => {
  	   return {
    	      ...curr,
              ...val
           };
         }, []);
  
  return [...acc, ...metadataReducer];
}, []);

console.log(dataReducer)

答案 4 :(得分:0)

您可以iterate像这样在两个数组上并将合并的值推入result

arr1.forEach(e => {
  arr2.forEach(e2 => {
    result.push(Object.assign({}, e, e2));
  });
});

其中之一也可以写成这样:

arr1.forEach(e => arr2.forEach(e2 => result.push(Object.assign({}, e, e2))));

const arr1 = [{
    "userId": 9
  },
  {
    "userId": 14
  }
];

const arr2 = [{
  "role": "1",
  "group": "3"
}, {
  "role": "1",
  "group": "2"
}];

const result = [];

arr1.forEach(e => {
  arr2.forEach(e2 => {
    result.push(Object.assign({}, e, e2));
  });
});

console.log(result);

答案 5 :(得分:0)

这是一种任意数量的对象数组的简短方法。

var array1 = [{ userId: 9 }, { userId: 14 }],
    array2 = [{ role: "1", group: "3" }, { role: "1", group: "2" }],
    result = [array1, array2]
        .reduce((a, b) =>
            a.reduce((r, v) =>
                r.concat(b.map(w => Object.assign({}, v, w))), []));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }