Combining JavaScript objects in an array with same

2019-01-27 03:06发布

问题:

I have been trying to combine 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 combine 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?

回答1:

This 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});
});


回答2:

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)


回答3:

Here's my tedious way: fiddle: 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);


回答4:

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>');