我的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}
]}
我希望合并所有相同的班次并将合并键的值添加到一起,并为项目创建新的Object以获得类似这样的内容
{"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);
}
});
但工作不正常。谢谢你。
答案 0 :(得分:3)
您可以使用哈希表作为对具有相同shiftId
的对象的引用,并返回包含收集和分组数据的新数组。
var data = { 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 }] },
result = {
rows: data.rows.reduce(function (hash) {
return function (r, a) {
if (!hash[a.shiftId]) {
hash[a.shiftId] = { shiftId: a.shiftId, shift: a.shift, item: [], value: 0 };
r.push(hash[a.shiftId]);
}
hash[a.shiftId].item.push({ itemName: a.item });
hash[a.shiftId].value += a.value;
return r;
};
}(Object.create(null)), [])
};
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
使用哈希表:
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]);
}
答案 2 :(得分:0)
const arr = [{
"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
}
];
const newArr = arr.reduce((acc, item, i) => {
if (!acc.length) { // First time
acc.push(item)
return acc;
}
if (acc[acc.length - 1].shiftId === item.shiftId) { // if current shiftId === last shiftId
if (!(acc[acc.length - 1].item instanceof Array)) {
acc[acc.length - 1].item = [{ // Convert item to be an array
"itemName": acc[acc.length - 1].item
}]
}
acc[acc.length - 1].item.push({ // Add current item name to the last item array
"itemName": item.item
});
acc[acc.length - 1].value = acc[acc.length - 1].value + item.value // value addition
} else { // If new shiftId
acc.push(item);
}
return acc;
}, []);
console.log(newArr);

答案 3 :(得分:0)
a = {"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}
]};
newa = {"rows":[]};
lastshiftid = -1;
newkey = -1;
for(key in a.rows){
curshiftid = a.rows[key].shiftId;
o = {"item":a.rows[key].item};
if(lastshiftid!=curshiftid){
newkey++;
a.rows[key].item = [];
newa.rows.push(a.rows[key]);
}
newa.rows[newkey].item.push(o);
lastshiftid = curshiftid;
}
console.log(newa.rows);