我一直在尝试将具有相同id值的数组内的对象组合在一起。我拥有的数组是这样的:
[
{"id" : "abcd","val1" : 1,"val2": 1, "val3" : 0},
{"id" : "abcd","val1" : 1,"val2": 1, "val3" : 1},
{"id" : "efgh","val1" : 0,"val2": 0, "val3" : 1}
]
我一直试图找到一种方法来组合这些,以便结果如下:
[
{"id" : "abcd","val1" : 2,"val2": 2, "val3" : 1},
{"id" : "efgh","val1" : 0,"val2": 0, "val3" : 1}
]
有没有办法用underscore.js
?
答案 0 :(得分:2)
这应该采用下划线的功能方法:
_.map(_.groupBy(arr, "id"), function(vals, id) {
return _.reduce(vals, function(m, o) {
for (var p in o)
if (p != "id")
m[p] = (m[p]||0)+o[p];
return m;
}, {id: id});
});
答案 1 :(得分:1)
标准的方法是将对象用作地图(此处为b
):
var b = {}, arr = [];
for (var id in a) {
var oa = a[id], ob = b[oa.id];
if (!ob) arr.push(ob = b[oa.id] = {});
for (var k in oa) ob[k] = k==='id' ? oa.id : (ob[k]||0)+oa[k];
}
console.log(arr)
答案 2 :(得分:0)
这是我乏味的方式: 小提琴:http://jsfiddle.net/hN8b6/5/
var a = [{
"id": "abcd",
"val1": 1,
"val2": 1,
"val3": 0
}, {
"id": "abcd",
"val1": 1,
"val2": 1,
"val3": 1
}, {
"id": "efgh",
"val1": 0,
"val2": 0,
"val3": 1
}];
// clone a
a2 = JSON.parse(JSON.stringify(a));
// make sure elements with the same id are next to each other
a2.sort(function (x, y) {
if (x['id'] < y['id']) {
return -1;
}
if (x['id'] > y['id']) {
return 1;
}
return 0;
});
// iterate over each one, if this one has the same id as the previous one, accumulate
// else add to b
var lastId;
var b = [];
for (var i = 0; i < a2.length; i++) {
if (lastId == a2[i]['id']) {
b[b.length-1]['val1'] += a2[i]['val1'];
b[b.length-1]['val2'] += a2[i]['val2'];
b[b.length-1]['val3'] += a2[i]['val3'];
} else {
b[b.length] = (a2[i]);
lastId = a2[i]['id'];
}
}
console.log(b);
答案 3 :(得分:0)
参考以下示例
var array = [{
"sequence" : 1,
"locationId" : "332228",
"lat" : 25.246511,
"lng" : 55.293837,
"stopName" : "332228",
"locationType" : "service",
"serviceType" : "Delivery",
"arrivalTime" : 37666,
"departureTime" : 37830,
"travelTime" : 1593,
"travelDistance" : 20985,
"travelCost" : 0,
"serviceTime" : 30,
"serviceTimeCost" : 0,
"tripNumber" : 0,
"orders" : [
{
"orderId" : "AQ137O1701240",
"SIZE1" : "28",
"SIZE2" : "520",
"SIZE3" : "52"
}
],
"stopId" : "SkhirG2ioZ"
},
{
"sequence" : 2,
"locationId" : "332228",
"lat" : 25.236407,
"lng" : 55.272403,
"stopName" : "332228",
"locationType" : "service",
"serviceType" : "Delivery",
"arrivalTime" : 38575,
"departureTime" : 38605,
"travelTime" : 1593,
"travelDistance" : 20985,
"travelCost" : 0,
"serviceTime" : 30,
"serviceTimeCost" : 0,
"tripNumber" : 0,
"orders" : [
{
"orderId" : "AQ137O1701233",
"SIZE1" : "23",
"SIZE2" : "402",
"SIZE3" : "30"
}
],
"stopId" : "H1iirfhisW"
},
{
"sequence" : 3,
"locationId" : "332228",
"lat" : 25.221368,
"lng" : 55.265915,
"stopName" : "332228",
"locationType" : "service",
"serviceType" : "Delivery",
"arrivalTime" : 39137,
"departureTime" : 39167,
"travelTime" : 974,
"travelDistance" : 5717,
"travelCost" : 0,
"serviceTime" : 30,
"serviceTimeCost" : 0,
"tripNumber" : 0,
"orders" : [
{
"orderId" : "AQ110O1705036",
"SIZE1" : "60",
"SIZE2" : "1046",
"SIZE3" : "68"
}
],
"stopId" : "H1csHM3jjb"
}]
var arrOut = [];
array.forEach(function(value) {
var existing = arrOut.filter(function(v, i) {
return v.locationId == value.locationId;
});
if (existing.length) {
var existingIndex = arrOut.indexOf(existing[0]);
arrOut[existingIndex].orders = arrOut[existingIndex].orders.concat(value.orders);
} else {
if(Array.isArray(value.orders)){
value.orders = value.orders
arrOut.push(value);
}
}
});
document.write('<pre>' + JSON.stringify(arrOut, 0, 4) + '</pre>');