admin管理员组文章数量:1279182
I have been trying to bine objects inside an array with same id-value. The array that I have is like this:
[
{"id" : "abcd","val1" : 1,"val2": 1, "val3" : 0},
{"id" : "abcd","val1" : 1,"val2": 1, "val3" : 1},
{"id" : "efgh","val1" : 0,"val2": 0, "val3" : 1}
]
I have been trying to find a way to bine these so that the result would be like this:
[
{"id" : "abcd","val1" : 2,"val2": 2, "val3" : 1},
{"id" : "efgh","val1" : 0,"val2": 0, "val3" : 1}
]
Is there some way to do this with underscore.js
?
I have been trying to bine objects inside an array with same id-value. The array that I have is like this:
[
{"id" : "abcd","val1" : 1,"val2": 1, "val3" : 0},
{"id" : "abcd","val1" : 1,"val2": 1, "val3" : 1},
{"id" : "efgh","val1" : 0,"val2": 0, "val3" : 1}
]
I have been trying to find a way to bine these so that the result would be like this:
[
{"id" : "abcd","val1" : 2,"val2": 2, "val3" : 1},
{"id" : "efgh","val1" : 0,"val2": 0, "val3" : 1}
]
Is there some way to do this with underscore.js
?
- 3 Did you try anything ? Where do you see a difficulty ? – Denys Séguret Commented Jun 6, 2014 at 7:30
- related: consolidating values in a list of objects based on an attribute which is the same – Bergi Commented Jun 6, 2014 at 7:37
- @Bergi You should probably write here a specific underscore answer, no ? Or do you think this question should be seen as a duplicate ? – Denys Séguret Commented Jun 6, 2014 at 7:42
- @dystroy: I'm on it :-) But I'm pretty confident that I already did something very similar before, only I can't find the duplicate. – Bergi Commented Jun 6, 2014 at 7:46
4 Answers
Reset to default 4This should do it with underscore's functional approach:
_.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});
});
The standard way to do that is to use an object as map (here 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)
Refer the below example
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>');
Here's my tedious way: fiddle: http://jsfiddle/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);
本文标签: Combining JavaScript objects in an array with same key valueStack Overflow
版权声明:本文标题:Combining JavaScript objects in an array with same key value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741266473a2368564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论