how to sum and get max value in array object with group by id javascript

Tags: javascript
Question!
var myArray = [
  {"Emiten_ID":'SMBR',"Lot":500,"Price":2500},     
  {"Emiten_ID":'SMBR',"Lot":300,"Price":2200},     
  {"Emiten_ID":'ELSA',"Lot":500,"Price":1000},
  {"Emiten_ID":'SMBR',"Lot":100,"Price":3000},     
  {"Emiten_ID":'BI',"Lot":300,"Price":500},     
  {"Emiten_ID":'AAI',"Lot":200,"Price":1300},
  {"Emiten_ID":'BTB',"Lot":700,"Price":2900},     
  {"Emiten_ID":'BI',"Lot":150,"Price":200},     
  {"Emiten_ID":'AAI',"Lot":200,"Price":600},
];

i want result like this, where i get sum from lot and max value from price

var Result= [
  {"Emiten_ID":'ELSA',"Lot":500,"Price":1000},
  {"Emiten_ID":'SMBR',"Lot":900,"Price":3000},     
  {"Emiten_ID":'BI',"Lot":450,"Price":500},     
  {"Emiten_ID":'BTB',"Lot":700,"Price":2900},     
  {"Emiten_ID":'AAI',"Lot":400,"Price":1300},
];


Answers

Using Map class available in ECMAScript 2015 (or polyfill):

var temp = new Map();

for (var item of myArray) {
    var e = temp.get(item.Emiten_ID)
    if (e) {
        e.Lot += item.Lot;
        e.Price = Math.max( e.Price, item.Price );
    } else {
        temp.set( item.Emiten_ID, 
            { Emiten_ID: item.Emiten_ID, Lot:item.Lot, Price:item.Price })
    }
}

var result = Array.from(temp.values());

console.log(result)


Let's try to decompose this. We'll start by writing a generic utility function which we will call combine, which combines properties on objects using a function specified for each property in a hash called combiners:

function combine(array, combiners) {
  const result = {};
  for (prop of Object.keys(combiners)) {
    result[prop] = combiners[prop](...array.map(elt => elt[prop]));
  }
  return result;
}

Example of using this:

combine(
  [{a: 1, b: 10}, {a: 42, b: 80}],
  {a: sum, b: Math.max}
)

which will result in

{a: 43, b: 80}

Of course, to make this work, we'll have to define sum:

function sum(...vals) { return vals.reduce(add); }

where add is just

function add(a, b) { return a + b; }

Next, we will group the input by the Emiten_ID property. You could use Underscore's _.groupBy for this, or write your own (see below).

const groups = _.groupBy(myArray, 'Emiten_ID`);

This will result in something looking like

{ SMBR: [
    { Emiten_ID: 'SMBR', "Lot": 500, "Price": 2500},     
    { Emiten_ID: 'SMBR', "Lot": 300, "Price": 2200}
  ],
  ELSA: [
  ]
}

After doing this prep work, it is pretty easy to get the result, by just mapping each value in groups using our combine utility:

const Result = Object.keys(groups).map(key =>
  combine(
    groups[key], 
    {
      Emiten_ID: identity, 
      Lot: sum, 
      Price: Math.max}));

where identity is just

function identity(id) { return id; }

If you'd prefer to abstract away the notion inside combine of mapping object properties, you could use some utility from Underscore again, or write it yourself:

function mapObject(obj, fn) {
  const result = {};

  for (prop of obj) result[prop] = fn(obj[prop], prop);
  return result;
}

Example of using this:

mapObject({a: 2, b: 3}, x => x * x)
// yields {a: 4, b: 9}

Now you can write combine a bit more simply, as

function combine(array, combiners) {
  return mapObject(combiners, function(combiner, prop) {
    return combiner(...array.map(elt => elt[prop]));
  };
}

In case you don't want to use Underscore's _.groupBy, here's a home grown version:

function groupBy(array, prop) {
  var result = {};
  array.forEach(elt => (result[elt[prop]] = result[elt[prop]] || []).push(elt));
  return result;
}


You could use Array#forEach and an object as hash table and group it by Emiten_ID.

var myArray = [{ "Emiten_ID": 'SMBR', "Lot": 500, "Price": 2500 }, { "Emiten_ID": 'SMBR', "Lot": 300, "Price": 2200 }, { "Emiten_ID": 'ELSA', "Lot": 500, "Price": 1000 }, { "Emiten_ID": 'SMBR', "Lot": 100, "Price": 3000 }, { "Emiten_ID": 'BI', "Lot": 300, "Price": 500 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 1300 }, { "Emiten_ID": 'BTB', "Lot": 700, "Price": 2900 }, { "Emiten_ID": 'BI', "Lot": 150, "Price": 200 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 600 }, ],
    result = [];

myArray.forEach(function (a) {
    if (!this[a.Emiten_ID]) {
        this[a.Emiten_ID] = { Emiten_ID: a.Emiten_ID, Lot: 0, Price: 0 };
        result.push(this[a.Emiten_ID]);
    }
    this[a.Emiten_ID].Lot += a.Lot;
    this[a.Emiten_ID].Price = Math.max(this[a.Emiten_ID].Price, a.Price);
}, Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6 with a closure of hash without use of this.

var myArray = [{ "Emiten_ID": 'SMBR', "Lot": 500, "Price": 2500 }, { "Emiten_ID": 'SMBR', "Lot": 300, "Price": 2200 }, { "Emiten_ID": 'ELSA', "Lot": 500, "Price": 1000 }, { "Emiten_ID": 'SMBR', "Lot": 100, "Price": 3000 }, { "Emiten_ID": 'BI', "Lot": 300, "Price": 500 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 1300 }, { "Emiten_ID": 'BTB', "Lot": 700, "Price": 2900 }, { "Emiten_ID": 'BI', "Lot": 150, "Price": 200 }, { "Emiten_ID": 'AAI', "Lot": 200, "Price": 600 }, ],
    result = [];

myArray.forEach((hash => a => {
    if (!hash[a.Emiten_ID]) {
        hash[a.Emiten_ID] = { Emiten_ID: a.Emiten_ID, Lot: 0, Price: 0 };
        result.push(hash[a.Emiten_ID]);
    }
    hash[a.Emiten_ID].Lot += a.Lot;
    hash[a.Emiten_ID].Price = Math.max(hash[a.Emiten_ID].Price, a.Price);
})(Object.create(null)));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }



This video can help you solving your question :)
By: admin