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

How to merge multiple jsons arrays into one array cross with sub array in JavaScript?

我有两个 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"}
]

您可以混合使用 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)

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()
)

您可以像这样 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);

另一种使用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

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

 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

这里是一个用于任意数量对象数组的简短方法。

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