任何人都可以帮我找到一种从对象数组返回对象的方法,以将所有对象连接到一个新的对象数组中,并使用对象名称而不是键。
array = [
1: {
cart: { fruits: {}, vegetables: {}, juices: {}}
},
2: {
cart: { boxes: {}, tools: {}, bottles: {}}
}
]
我正在尝试使用方法和计算属性在 Vuejs 中获取此特定输出,但我只能使用键作为普通数组获取输出。我需要对象名称而不是键。
预期输出
newArray = {
cart: { fruits: {}, vegetables: {}, juices: {} boxes: {}, tools: {}, bottles: {}}
}
最佳答案
您可以使用reduce和 destructuring assignment并使用键以所需的格式合并对象。
let array = [{cart: { fruits: {}, vegetables: {}, juices: {}}},{cart: { boxes: {}, tools: {}, bottles: {}}}]
let op = array.reduce( (op,inp) => {
let key = Object.keys(inp)[0]
op[key] = {...op[key], ...inp[key]}
return op
},{})
console.log(op)
关于javascript - VueJs - 将数组中的对象检索到新的对象数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55007745/