使用javascript在json对象内部合并json对象并创建新对象

使用javascript在json对象内部合并json对象并创建新对象,javascript,json,Javascript,Json,我的JSON看起来像这样 {"rows":[ {"shiftId":1,"shift":"Morning","item":"Tea","value":20}, {"shiftId":1,"shift":"Morning","item":"Coffee","value":30}, {"shiftId":2,"shift":"Evening","item":"Tea","value":40}, {"shiftId":2,"shift":"Evening","item"

我的JSON看起来像这样

{"rows":[
    {"shiftId":1,"shift":"Morning","item":"Tea","value":20},
    {"shiftId":1,"shift":"Morning","item":"Coffee","value":30},
    {"shiftId":2,"shift":"Evening","item":"Tea","value":40},
    {"shiftId":2,"shift":"Evening","item":"Coffee","value":35}
]}
{"rows":[
    {
     "shiftId":1,
     "shift":"Morning",
     "item":[{"itemName":"Tea"},{"itemName":"Coffee"}],
     "value":50
     },
    {
    "shiftId":2,
    "shift":"Evening",
    "item":[{"itemName":"Tea"},{"itemName":"Coffee"}],
    "value":75
    }
]}
    var merged = {rows: []};
                data.forEach(function (source) {
                    if (!merged.rows.some(function (row) {
                            return row.shiftId == source.shiftId;
                        })) {
                        merged.rows.push({
                            shiftId: source.shift,
                            shift: source.shift,
                            item: [{
                                itemName: source.shift
                            }],
                            value: source.value
                        });
                    } else {
                        var existRow = merged.rows.filter(function (existRow) {
                            return existRow.shiftId == source.shiftId
                        })[0];
                        existRow.total += source.total;
                        existRow.item = source.item.push(existRow.item);
                    }
                });
我希望合并所有相同的移位,并将合并键的值添加到一起,为item创建新的对象,以获得如下所示的效果

{"rows":[
    {"shiftId":1,"shift":"Morning","item":"Tea","value":20},
    {"shiftId":1,"shift":"Morning","item":"Coffee","value":30},
    {"shiftId":2,"shift":"Evening","item":"Tea","value":40},
    {"shiftId":2,"shift":"Evening","item":"Coffee","value":35}
]}
{"rows":[
    {
     "shiftId":1,
     "shift":"Morning",
     "item":[{"itemName":"Tea"},{"itemName":"Coffee"}],
     "value":50
     },
    {
    "shiftId":2,
    "shift":"Evening",
    "item":[{"itemName":"Tea"},{"itemName":"Coffee"}],
    "value":75
    }
]}
    var merged = {rows: []};
                data.forEach(function (source) {
                    if (!merged.rows.some(function (row) {
                            return row.shiftId == source.shiftId;
                        })) {
                        merged.rows.push({
                            shiftId: source.shift,
                            shift: source.shift,
                            item: [{
                                itemName: source.shift
                            }],
                            value: source.value
                        });
                    } else {
                        var existRow = merged.rows.filter(function (existRow) {
                            return existRow.shiftId == source.shiftId
                        })[0];
                        existRow.total += source.total;
                        existRow.item = source.item.push(existRow.item);
                    }
                });
我正试着这样做

{"rows":[
    {"shiftId":1,"shift":"Morning","item":"Tea","value":20},
    {"shiftId":1,"shift":"Morning","item":"Coffee","value":30},
    {"shiftId":2,"shift":"Evening","item":"Tea","value":40},
    {"shiftId":2,"shift":"Evening","item":"Coffee","value":35}
]}
{"rows":[
    {
     "shiftId":1,
     "shift":"Morning",
     "item":[{"itemName":"Tea"},{"itemName":"Coffee"}],
     "value":50
     },
    {
    "shiftId":2,
    "shift":"Evening",
    "item":[{"itemName":"Tea"},{"itemName":"Coffee"}],
    "value":75
    }
]}
    var merged = {rows: []};
                data.forEach(function (source) {
                    if (!merged.rows.some(function (row) {
                            return row.shiftId == source.shiftId;
                        })) {
                        merged.rows.push({
                            shiftId: source.shift,
                            shift: source.shift,
                            item: [{
                                itemName: source.shift
                            }],
                            value: source.value
                        });
                    } else {
                        var existRow = merged.rows.filter(function (existRow) {
                            return existRow.shiftId == source.shiftId
                        })[0];
                        existRow.total += source.total;
                        existRow.item = source.item.push(existRow.item);
                    }
                });
但是工作不正常。提前感谢。

使用哈希表:

var hash={};
var input={"rows":[
{"shiftId":1,"shift":"Morning","item":"Tea","value":20},
{"shiftId":1,"shift":"Morning","item":"Coffee","value":30},
{"shiftId":2,"shift":"Evening","item":"Tea","value":40},
{"shiftId":2,"shift":"Evening","item":"Coffee","value":35}
]}.rows;

input.forEach(function(row){
  var el=(hash[row.shiftId]=hash[rows.shiftId]||{shiftId:row.shiftId,shift:row.shift,items:[],value:0});
   el.items.push({itemName:row.itemName});
   el.value+=row.value;
});
现在,您可以这样创建结果:

var result={rows:[]};
for(key in hash){
 result.rows.push(hash[key]);
}
使用哈希表:

var hash={};
var input={"rows":[
{"shiftId":1,"shift":"Morning","item":"Tea","value":20},
{"shiftId":1,"shift":"Morning","item":"Coffee","value":30},
{"shiftId":2,"shift":"Evening","item":"Tea","value":40},
{"shiftId":2,"shift":"Evening","item":"Coffee","value":35}
]}.rows;

input.forEach(function(row){
  var el=(hash[row.shiftId]=hash[rows.shiftId]||{shiftId:row.shiftId,shift:row.shift,items:[],value:0});
   el.items.push({itemName:row.itemName});
   el.value+=row.value;
});
现在,您可以这样创建结果:

var result={rows:[]};
for(key in hash){
 result.rows.push(hash[key]);
}

您可以使用哈希表作为对具有相同
shiftId
的对象的引用,并返回一个包含收集和分组数据的新数组

var数据={rows:[{shiftId:1,班次:“早上”,项目:“茶”,值:20},{shiftId:1,班次:“早上”,项目:“咖啡”,值:30},{shiftId:2,班次:“晚上”,项目:“茶”,值:40},{shiftId:2,班次:“晚上”,项目:“咖啡”,值:35},
结果={
行:data.rows.reduce(函数(散列){
返回函数(r,a){
if(!hash[a.shiftId]){
hash[a.shiftId]={shiftId:a.shiftId,shift:a.shift,项:[],值:0};
r、 push(hash[a.shiftId]);
}
hash[a.shiftId].item.push({itemName:a.item});
hash[a.shiftId].value+=a.value;
返回r;
};
}(Object.create(null)),[])
};
控制台日志(结果)

.as console wrapper{max height:100%!important;top:0;}
您可以使用哈希表作为对具有相同
shiftId
的对象的引用,并返回包含收集和分组数据的新数组

var数据={rows:[{shiftId:1,班次:“早上”,项目:“茶”,值:20},{shiftId:1,班次:“早上”,项目:“咖啡”,值:30},{shiftId:2,班次:“晚上”,项目:“茶”,值:40},{shiftId:2,班次:“晚上”,项目:“咖啡”,值:35},
结果={
行:data.rows.reduce(函数(散列){
返回函数(r,a){
if(!hash[a.shiftId]){
hash[a.shiftId]={shiftId:a.shiftId,shift:a.shift,项:[],值:0};
r、 push(hash[a.shiftId]);
}
hash[a.shiftId].item.push({itemName:a.item});
hash[a.shiftId].value+=a.value;
返回r;
};
}(Object.create(null)),[])
};
控制台日志(结果)
.as控制台包装{最大高度:100%!重要;顶部:0;}
使用

const arr=[{
"shiftId":1,,
“班次”:“早上”,
“物品”:“茶”,
“价值”:20
},
{
"shiftId":1,,
“班次”:“早上”,
“物品”:“咖啡”,
“价值”:30
},
{
"shiftId":2,,
“班次”:“晚上”,
“物品”:“茶”,
“价值”:40
},
{
"shiftId":2,,
“班次”:“晚上”,
“物品”:“咖啡”,
“价值”:35
}
];
const newArr=arr.reduce((附件,项目,i)=>{
如果(!acc.length){//第一次
附件推送(项目)
返回acc;
}
if(acc[acc.length-1].shiftId==item.shiftId){//if当前shiftId==last shiftId
if(!(附件[acc.length-1]。阵列的项目实例)){
acc[acc.length-1]。项=[{//将项转换为数组
“项目名称”:附件[acc.length-1]。项目
}]
}
acc[acc.length-1].item.push({//将当前项名称添加到最后一个项数组
“itemName”:item.item
});
acc[acc.length-1]。value=acc[acc.length-1]。value+item.value//增值
}else{//如果是新的shiftId
acc.push(项目);
}
返回acc;
}, []);
控制台日志(newArr)使用

const arr=[{
"shiftId":1,,
“班次”:“早上”,
“物品”:“茶”,
“价值”:20
},
{
"shiftId":1,,
“班次”:“早上”,
“物品”:“咖啡”,
“价值”:30
},
{
"shiftId":2,,
“班次”:“晚上”,
“物品”:“茶”,
“价值”:40
},
{
"shiftId":2,,
“班次”:“晚上”,
“物品”:“咖啡”,
“价值”:35
}
];
const newArr=arr.reduce((附件,项目,i)=>{
如果(!acc.length){//第一次
附件推送(项目)
返回acc;
}
if(acc[acc.length-1].shiftId==item.shiftId){//if当前shiftId==last shiftId
if(!(附件[acc.length-1]。阵列的项目实例)){
acc[acc.length-1]。项=[{//将项转换为数组
“项目名称”:附件[acc.length-1]。项目
}]
}
acc[acc.length-1].item.push({//将当前项名称添加到最后一个项数组
“itemName”:item.item
});
acc[acc.length-1]。value=acc[acc.length-1]。value+item.value//增值
}else{//如果是新的shiftId
acc.push(项目);
}
返回acc;
}, []);
控制台日志(newArr)
小提琴:


小提琴手:

到目前为止,你尝试了什么?到目前为止,您为尝试此功能而编写的代码是什么?我建议您阅读。我正在尝试。感谢您的快速回复@selten98谢谢@evolutionxbox“有人能帮我吗”。。。首先,你需要通过展示你已经尝试过的东西来帮助自己。Stackoverflow不是免费的代码编写服务。这里的目标是帮助您修复代码到目前为止您尝试了什么?到目前为止,您为尝试此功能而编写的代码是什么?我建议您阅读。我正在尝试。感谢您的快速回复@selten98谢谢@evolutionxbox“有人能帮我吗”。。。首先,你需要通过展示你已经尝试过的东西来帮助自己。Stackoverflow不是免费的代码编写服务。这里的目标是帮助您修复代码为什么使用
(Object.create(null))
而不是
({})
?是保证没有继承的属性吗?@evolutionxbox,因为如果你有一个像
toString
这样的id,那么它已经在对象中了,