Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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},
];
like image 784
muhammad furqon Avatar asked Sep 20 '16 05:09

muhammad furqon


Video Answer


4 Answers

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; }
like image 153
Nina Scholz Avatar answered Oct 26 '22 17:10

Nina Scholz


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

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 }, ]

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)
like image 32
Burt_Harris Avatar answered Oct 26 '22 17:10

Burt_Harris


You can use linq.js library:

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},
];
var answer = Enumerable.From(myArray).GroupBy("x => x.Emiten_ID", "x => {Lot: x.Lot, Price: x.Price}").Select("x => {Emiten_ID:x.Key(), Lot:x.Sum(y=>y.Lot), Price:x.Max(y=>y.Price)}").ToArray();
answer.forEach(x => console.log(x));
<script data-require="[email protected]" data-semver="3.0.0" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.js"></script>
<script data-require="[email protected]+2" data-semver="2.2.0+2" src="//cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.js"></script>
<script data-require="[email protected]+2" data-semver="2.2.0+2" src="//cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/jquery.linq.js"></script>
like image 22
Slava Utesinov Avatar answered Oct 26 '22 16:10

Slava Utesinov


this is long way to this, hope there is simpler way do 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},
];

var sortedIds = [];
var Result = [];
function sortArray(){

    for(var i = 0; i < myArray.length; i++){
        if(myArray.indexOf(myArray[i].Emiten_ID) < 0){
            sortedIds.push(myArray[i].Emiten_ID);
            Result.push({
                "Emiten_ID" : myArray[i].Emiten_ID,
                "Lot"       : sumLot(myArray[i].Emiten_ID),
                "Price"     : maxPrice(myArray[i].Emiten_ID);
            });


        }
    }   

    //out put
    console.log(Result);
}

function sumLot(id){
    var sum = 0;
    for(var i = 0; i < myArray.length; i++){
        if(myArray[i].Emiten_ID == id){
            sum += myArray[i].lot;
        }
    }   
    return sum;
}

function maxPrice(id){
    var max = 0;
    for(var i = 0; i < myArray.length; i++){
        if(myArray[i].Price > max){
            max = myArray[i].Price;
        }
    }   
    return max;
}
like image 42
Azad Avatar answered Oct 26 '22 17:10

Azad