3

for example i have 5 objects:

{ row: aa, col: 1, value: 1 }
{ row: bb, col: 2, value: 1 }
{ row: bb, col: 3, value: 1 }
{ row: aa, col: 1, value: 1 }
{ row: aa, col: 2, value: 1 }

i want to sum values if row and col are the same, so the output should be:

{ row: aa, col: 1, value: 2 }
{ row: bb, col: 2, value: 1 }
{ row: bb, col: 3, value: 1 }
{ row: aa, col: 2, value: 1 }

thank you for your help!

tried this: Sum javascript object propertyA values with same object propertyB in array of objects

Community
  • 1
  • 1
Bato Dor
  • 841
  • 3
  • 11
  • 33
  • @DanielShillcock updated my question – Bato Dor Oct 04 '16 at 16:00
  • 3
    Can you show how what you've tried applies to your question, otherwise we won't know at what point you're having problems. – James Thorpe Oct 04 '16 at 16:03
  • Please create an [MCVE](http://stackoverflow.com/help/mcve). See also http://www.sscce.org/. You must show the efforts that you _yourself_ have taken towards solving this. No one here is obliged to give you a solution from scratch, but we will help you improve upon what you have tried. – Oka Oct 04 '16 at 16:04
  • @Oka i tried almost the half of the day solving this, and i couldnt go far than the answer from the example i've given, and yes u're not obligated to solve this – Bato Dor Oct 04 '16 at 16:10

3 Answers3

6

You can do this with reduce() and one object to store keys.

var data = [
  { row: 'aa', col: 1, value: 1 },
  { row: 'bb', col: 2, value: 1 },
  { row: 'bb', col: 3, value: 1 },
  { row: 'aa', col: 1, value: 1 },
  { row: 'aa', col: 2, value: 1 }
]

var o = {}
var result = data.reduce(function(r, e) {
  var key = e.row + '|' + e.col;
  if (!o[key]) {
    o[key] = e;
    r.push(o[key]);
  } else {
    o[key].value += e.value;
  }
  return r;
}, []);

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
3

Just for completeness, with a version for variable keys, an object for grouping the parts and Array#forEach.

var data = [{ row: 'aa', col: 1, value: 1 }, { row: 'bb', col: 2, value: 1 }, { row: 'bb', col: 3, value: 1 }, { row: 'aa', col: 1, value: 1 }, { row: 'aa', col: 2, value: 1 }],
    grouped = [];

data.forEach(function (a) {
    var key = ['row', 'col'].map(function (k) { return a[k]; }).join('|');
    if (!this[key]) {
        this[key] = { row: a.row, col: a.col, value: 0 };
        grouped.push(this[key]);
    }
    this[key].value += a.value;
}, Object.create(null));

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

What I would do is put your objects in an array then iterate over that and check on each iteration if the key of a new object matches that of an old one and load the objects into a separate array if there isn't a match. If it does match then add its value to the value of the old own. I tested the following code and it seems to work how you want.

 var array = [{ row: 'aa', col: 1, value: 1 },
         { row: 'bb', col: 2, value: 1 },
         { row: 'bb', col: 3, value: 1 },
         { row: 'aa', col: 1, value: 1 },
         { row: 'aa', col: 2, value: 1 }];

 var newArray = [];

 for(var x in array) {
    for(var y in newArray) {
        var found = false;
        if(array[x].row == newArray[y].row && array[x].col == newArray[y].col) {
             newArray[y].value += array[x].value;
             found = true;
             break;
        }
    }
    if(!found) {
          newArray.push(array[x]);
    }
 }

 console.log(newArray);