根据其中的数组合并数组中的 javascript 个对象
Merge javascript objects in an array based on an array inside it
将数组重组为输出的最佳方法是什么?我需要将所有值键(无论是否为数组)合并到共享相同名称键的对象中。这里有类似的东西,但这不能回答我的问题,因为我也有数组。
var array = [{
VULN: [{random1:"asd11",random2:"asd12"}, {random3:"asd23",random4:"asd24"}]
}, {
VULN: [{random5:"asd35",random6:"asd36"}, {random7:"asd47",random8:"asd43"}]
}, {
VULN: [{random9:"asd55",random10:"asd51"}, {random11:"asd56",random12:"asd63"}]
}];
至
VULN=[{random1:"asd11",random2:"asd12"}, {random3:"asd23",random4:"asd24"},{name:"asd3",value:"asd3"}, {random5:"asd35",random6:"asd36"}, {random7:"asd47",random8:"asd43"}, {random9:"asd55",random10:"asd51"}, {random11:"asd56",random12:"asd63"}]
这是你想要的:
var array = [{
VULN: [{ name: "asd1", value: "asd1" }, { name: "asd2", value: "asd2" }]
}, {
VULN: [{ name: "asd3", value: "asd3" }, { name: "asd4", value: "asd4" }]
}, {
VULN: [{ name: "asd5", value: "asd5" }, { name: "asd6", value: "asd6" }]
}];
console.log(array.map(x => x.VULN).flat())
使用flatMap:
array.flatMap(x => x.VULN);
使用lodash
lodash.flatten(array.map(x => x.VULN))
将数组重组为输出的最佳方法是什么?我需要将所有值键(无论是否为数组)合并到共享相同名称键的对象中。这里有类似的东西,但这不能回答我的问题,因为我也有数组。
var array = [{
VULN: [{random1:"asd11",random2:"asd12"}, {random3:"asd23",random4:"asd24"}]
}, {
VULN: [{random5:"asd35",random6:"asd36"}, {random7:"asd47",random8:"asd43"}]
}, {
VULN: [{random9:"asd55",random10:"asd51"}, {random11:"asd56",random12:"asd63"}]
}];
至
VULN=[{random1:"asd11",random2:"asd12"}, {random3:"asd23",random4:"asd24"},{name:"asd3",value:"asd3"}, {random5:"asd35",random6:"asd36"}, {random7:"asd47",random8:"asd43"}, {random9:"asd55",random10:"asd51"}, {random11:"asd56",random12:"asd63"}]
这是你想要的:
var array = [{
VULN: [{ name: "asd1", value: "asd1" }, { name: "asd2", value: "asd2" }]
}, {
VULN: [{ name: "asd3", value: "asd3" }, { name: "asd4", value: "asd4" }]
}, {
VULN: [{ name: "asd5", value: "asd5" }, { name: "asd6", value: "asd6" }]
}];
console.log(array.map(x => x.VULN).flat())
使用flatMap:
array.flatMap(x => x.VULN);
使用lodash
lodash.flatten(array.map(x => x.VULN))