在typescript中合并具有相同值的JSON数据
我有一个从URL获取的JSON数据。我想合并具有相同值的数据,下面是示例数据。我在网上搜索过,但没有找到答案。我只找到了相同的键在typescript中合并具有相同值的JSON数据,json,angular,typescript,Json,Angular,Typescript,我有一个从URL获取的JSON数据。我想合并具有相同值的数据,下面是示例数据。我在网上搜索过,但没有找到答案。我只找到了相同的键 [ { "banana": [ { "color": yellow, "size": "small" }, { "size": "medium" }, { "size": "large" } ], "pro
[ {
"banana": [
{
"color": yellow,
"size": "small"
},
{
"size": "medium"
},
{
"size": "large"
}
],
"process_name": "fruits"},{
"carnivores": [
{
"name": "lion"
},
{
"name": "tiger"
},
{
"name": "chetah"
},
{
"name": "dianosaur"
}
],
"process_name": "animal"}, {
"apple": [
{
"color": red,
"size": "large"
}
],
"process_name": "fruits"}]
我想将“process\u name”:“fruits”
的数据合并到一个数组中,如下所示,结果应该是
[{
"banana": [
{
"color": yellow,
"size": "small"
},
{
"size": "medium"
},
{
"size": "large"
}
],
"apple": [
{
"color": red,
"size": "large"
}
],
"process_name": "fruits" }, {
"carnivores": [
{
"name": "lion"
},
{
"name": "tiger"
},
{
"name": "chetah"
},
{
"name": "dianosaur"
}
],
"process_name": "animal"}]
有人能帮上忙吗?先来点小意思:
//数据定义
const json_before=[{
“香蕉”:[
{
“颜色”:“黄色”,
“大小”:“小”
},
{
“大小”:“中等”
},
{
“大小”:“大”
}
],
“过程名称”:“果实”},
{
“食肉动物”:[
{
“名称”:“狮子”
},
{
“名称”:“老虎”
},
{
“名称”:“chetah”
},
{
“名称”:“dianosaur”
}
],
“过程名称”:“动物”},
{
“苹果”:[
{
“颜色”:“红色”,
“大小”:“大”
}
],
“过程名称”:“果实”
}];
//加工
const json_after=json_before.reduce((arr,next)=>{
const exist=arr.find(el=>el.process\u name===next.process\u name);
if(存在)Object.assign(存在,下一个);
else arr.push(下一步);
返回arr;
}, []);
//检查
log(json_之后)代码>只需开始一些代码片段:
//数据定义
const json_before=[{
“香蕉”:[
{
“颜色”:“黄色”,
“大小”:“小”
},
{
“大小”:“中等”
},
{
“大小”:“大”
}
],
“过程名称”:“果实”},
{
“食肉动物”:[
{
“名称”:“狮子”
},
{
“名称”:“老虎”
},
{
“名称”:“chetah”
},
{
“名称”:“dianosaur”
}
],
“过程名称”:“动物”},
{
“苹果”:[
{
“颜色”:“红色”,
“大小”:“大”
}
],
“过程名称”:“果实”
}];
//加工
const json_after=json_before.reduce((arr,next)=>{
const exist=arr.find(el=>el.process\u name===next.process\u name);
if(存在)Object.assign(存在,下一个);
else arr.push(下一步);
返回arr;
}, []);
//检查
log(json_之后)代码>