在javascript中合并数组本身

在javascript中合并数组本身,javascript,arrays,Javascript,Arrays,我的数组如下所示: {"type":"send","name":"kebab","quantity":"1"}, {"type":"send","name":"potato","quantity":"25000"}, {"type":"receive","name":"money","quantity":"1"}, {"type":"receive","name":"soul","quantity":"12"}, {"type":"receive","name":"paper","quantit

我的数组如下所示:

{"type":"send","name":"kebab","quantity":"1"},
{"type":"send","name":"potato","quantity":"25000"},
{"type":"receive","name":"money","quantity":"1"},
{"type":"receive","name":"soul","quantity":"12"},
{"type":"receive","name":"paper","quantity":"8"},
{"type":"send","name":"kebab","quantity":"1"},
{"type":"send","name":"potato","quantity":"25000"},
{"type":"receive","name":"money","quantity":"1"},
{"type":"receive","name":"soul","quantity":"12"},
{"type":"receive","name":"paper","quantity":"8"}
{"type":"send","name":"kebab","quantity":"2"},
{"type":"send","name":"potato","quantity":"50000"},
{"type":"receive","name":"money","quantity":"2"},
{"type":"receive","name":"soul","quantity":"24"},
{"type":"receive","name":"paper","quantity":"16"}
我希望它合并到一个新的数组中,在这个数组中可以加上或减去值,如下所示:

{"type":"send","name":"kebab","quantity":"1"},
{"type":"send","name":"potato","quantity":"25000"},
{"type":"receive","name":"money","quantity":"1"},
{"type":"receive","name":"soul","quantity":"12"},
{"type":"receive","name":"paper","quantity":"8"},
{"type":"send","name":"kebab","quantity":"1"},
{"type":"send","name":"potato","quantity":"25000"},
{"type":"receive","name":"money","quantity":"1"},
{"type":"receive","name":"soul","quantity":"12"},
{"type":"receive","name":"paper","quantity":"8"}
{"type":"send","name":"kebab","quantity":"2"},
{"type":"send","name":"potato","quantity":"50000"},
{"type":"receive","name":"money","quantity":"2"},
{"type":"receive","name":"soul","quantity":"24"},
{"type":"receive","name":"paper","quantity":"16"}
我不知道该怎么做

更新:类型和名称应保持不变,只更改数量

您可以将项目下放到新数组中,并将数量相加

const items=[{“type”:“send”,“name”:“kebab”,“quantity”:“1”},
{“类型”:“发送”,“名称”:“土豆”,“数量”:“25000”},
{“类型”:“接收”,“名称”:“货币”,“数量”:“1”},
{“类型”:“接收”,“名称”:“灵魂”,“数量”:“12”},
{“类型”:“接收”,“名称”:“纸张”,“数量”:“8”},
{“类型”:“发送”,“名称”:“烤肉串”,“数量”:“1”},
{“类型”:“发送”,“名称”:“土豆”,“数量”:“25000”},
{“类型”:“接收”,“名称”:“货币”,“数量”:“1”},
{“类型”:“接收”,“名称”:“灵魂”,“数量”:“12”},
{“类型”:“接收”,“名称”:“纸张”,“数量”:“8”}]
让结果=项目。减少((arr,项目)=>{
//按名称在新数组中查找项
let found=arr.find(i=>i.name==item.name&&i.type==item.type)
//如果该项不存在,请将其添加到数组中
如果(!找到)返回arr.concat(项目)
//如果项目确实存在,则将两个数量相加
//这将就地修改值,因此我们不需要重新添加它
found.quantity=parseFloat(item.quantity)+parseFloat(found.quantity)
//返回数组的新状态
返回arr;
}, [])

console.log(result)
您可以使用
reduce
将数组分组到一个对象中。使用
Object.values
将对象转换为数组

var arr=[{“类型”:“发送”,“名称”:“烤肉串”,“数量”:“1”},{“类型”:“发送”,“名称”:“土豆”,“数量”:“25000”},{“类型”:“接收”,“名称”:“货币”,“数量”:“1”},{“类型”:“接收”,“名称”:“灵魂”,“数量”:“12”},{“类型”:“接收”,“名称”:“纸张”,“数量”:“8”},{“类型”:“发送”,“名称”:“烤肉串”,“数量”:“1”},{“类型”:“发送”,“名称”:“土豆”,“数量”:“数量”:“数量”:“发送”,“数量”:“25000”},{“类型”:“接收”,“名称”:“货币”,“数量”:“1”},{“类型”:“接收”,“名称”:“灵魂”,“数量”:“12”},{“类型”:“接收”,“名称”:“纸张”,“数量”:“8”});
var结果=对象值(arr.reduce((c,v)=>{
c[v.name]=c[v.name]|{类型:,名称:v.name,数量:0};
c[v.name].quantity+=+v.quantity;//更新数量
c[v.name].type=v.type;//更新类型
返回c;
}, {}));

console.log(结果)
到目前为止你都做了些什么?我可以问一下你为什么要投否决票吗?如果你想赢得一些声誉,有很多糟糕的答案。是的,我也是。结果与提供的预期结果完全相同。
1*+1==5*-1
;)我想他们不希望人们回答原始海报没有回答的问题显示出任何努力…如果这是真的,他们应该否决问题而不是答案。我认为他们都这样做,以迫使用户删除他们的帖子。这也让我感到困惑:)@Ivan有些用户认为否决正确答案是个好主意,因为他们不喜欢这个问题…@Ivan使用你的否决权来恢复这种不良行为并独立回答正确。