有这样两组数据
var arr1 = [{ "a": 1, "b": 2, "c": 3 }];
var arr2 = [{ "d": 4, "f": 5 }];
怎么拼成下面这样
var arr3 = [
{"a": 1, "d": 4},
{"a": 1, "f": 5},
{"b": 2, "d": 4},
{"b": 2, "f": 5},
{"c": 3, "f": 5}
]
有这样两组数据
var arr1 = [{ "a": 1, "b": 2, "c": 3 }];
var arr2 = [{ "d": 4, "f": 5 }];
怎么拼成下面这样
var arr3 = [
{"a": 1, "d": 4},
{"a": 1, "f": 5},
{"b": 2, "d": 4},
{"b": 2, "f": 5},
{"c": 3, "f": 5}
]
const arr1 = [{ "a": 1, "b": 2, "c": 3 }];
const arr2 = [{ "d": 4, "f": 5 }];
function cartesian(arr1, arr2) {
const entries2 = Object.entries(arr1[0]);
return Object.entries(arr2[0])
.flatMap(([key, value]) => entries2.map(([k, v]) => ({ [key]: value, [k]: v })));
}
console.log(cartesian(arr2, arr1));
10 回答11.3k 阅读
5 回答4.9k 阅读✓ 已解决
4 回答3.2k 阅读✓ 已解决
2 回答2.8k 阅读✓ 已解决
3 回答5.2k 阅读✓ 已解决
1 回答3.4k 阅读✓ 已解决
3 回答2.4k 阅读✓ 已解决
如果是多个怎么合并没有说,这里给出单项合并