KARTHI SRV KARTHI SRV - 2 months ago 4
Javascript Question

Object into array conversion using JavaScript

In these I need to add the array value in which pensionname are the same. Add all amount if pensionName value are the same and rechange the array into myoutput array.

My main array:

Object, Object, Object, Object, Object, Object, Object]
Object
amount: 2000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_1"
Object
amount: 2000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_2"
Object
amount: 2000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_3"
Object
amount: 2000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_4"
Object
amount: 2000
member: "2"
name: "Peter Andrews"
pensionName: "2A"
rowId: "row_5"
Object
amount: 2000
member: "2"
name: "Peter Andrews"
pensionName: "2A"
rowId: "row_6"
Object
amount: 2000
member: "2"
name: "Peter Andrews"
pensionName: "2A"
rowId: "row_7"


I need the output as:

[Object, Object]
Object
amount: 8000
member: "1"
name: "Peter Andrews"
pensionName: "1A"
rowId: "row_1"

Object
amount: 6000
member: "2"
name: "Peter Andrews"
pensionName: "2A"
rowId: "row_2"


So I had done:

Enter code here
_.forEach(pension,function(item) {
key = item['pensionName'];
if (key != "0" && key !=""){
if (typeof(groups[key]) == "undefined") {
count++;
str = "row_"+count;
groups[key] = {'member' : item['member'],'name' : item['name'],'pensionName' : item['pensionName'],'amount' : item['amount'],'rowId' : str};
}
else {
groups[key]['amount'] = parseFloat(groups[key]['amount']) + parseFloat(item['amount']);
}
}
});


It resulted in:

1A:Object, 2A: Object]
1A: Object
amount :200
member :1
name : "hello"
name2 : 1A
id : "row_1"
2A: Object
amount : 200
member : 2
name : "hello"
name2 : "2A"
id : "row_2"


But I can't push it into an array:

var pensionBalance = getPension();
pensionBalance.map(function(v){
arr.push(v);
});


So how can I convert it into an array and push or any other method to add the array value?

Answer

Maybe this works for you, but I am not sure, what to do with rowId property. Take all or just the first?

Basically this solution works with the thisArg, here this from Array#forEach. this is used as a temporary object with references to array items of unique persons in grouped.

In the forEach loop, first it checks if the person is already in the grouped array and therfore in this. If not, a new object is generated and to a new property with the actual pensionName to this assigned and pushed to grouped.

Now we can access this object with the key of pensionName and add the amount and push rowId to the array for it.

temporary this object:

{
    "1A": {                       // generated in first loop
        "amount": 8000,
        "member": "1",
        "name": "Peter Andrews",
        "pensionName": "1A",
        "rowId": [
            "row_1",              // added in first
            "row_2",              // ... second
            "row_3",              // third
            "row_4"               // forth
        ]
    },
    "2A": {                       // generated in fifth loop
        "amount": 6000,
        "member": "2",
        "name": "Peter Andrews",
        "pensionName": "2A",
        "rowId": [
            "row_5",              // added in fifth
            "row_6",              // ... sixth
            "row_7"               // seventh
        ]
    }
}

var object = [{ amount: 2000, member: "1", name: "Peter Andrews", pensionName: "1A", rowId: "row_1" }, { amount: 2000, member: "1", name: "Peter Andrews", pensionName: "1A", rowId: "row_2" }, { amount: 2000, member: "3", name: "Peter Andrews", pensionName: "1A", rowId: "row_3" }, { amount: 2000, member: "1", name: "Peter Andrews", pensionName: "1A", rowId: "row_4" }, { amount: 2000, member: "2", name: "Peter Andrews", pensionName: "2A", rowId: "row_5" }, { amount: 2000, member: "2", name: "Peter Andrews", pensionName: "2A", rowId: "row_6" }, { amount: 2000, member: "2", name: "Peter Andrews", pensionName: "2A", rowId: "row_7" }],
    grouped = [];

object.forEach(function (a) {
    if (!this[a.pensionName]) {
        this[a.pensionName] = { amount: 0, member: a.member, name: a.name, pensionName: a.pensionName, rowId: [] };
        grouped.push(this[a.pensionName]);
    }
    this[a.pensionName].amount += a.amount;
    this[a.pensionName].rowId.push(a.rowId);
}, {});

document.write('<pre>' + JSON.stringify(grouped, 0, 4) + '</pre>');