pardeep grover
pardeep grover

Reputation: 77

Combine object value

My jquery array is showing like

[Object { qty=1, item_id="76", add_ons="2", add_on_price:20}, Object { qty=1, item_id="76", add_ons="1",add_on_price:40}]

I want to make an array like this [object{ qty=2,item_id=76,add_ons_price=60}]

I need to add the qty,add_ons_price in single object.

any help will be appreciated.

Upvotes: 0

Views: 36

Answers (1)

SkillsIndexOutOfBounds
SkillsIndexOutOfBounds

Reputation: 539

this will group objects based on property item_id

var result = {};

for (var i = 0, len = myObjects.length; i < len; i++) {
    var obj = myObjects[i];

    if (result[obj.item_id] === undefined) {
        result[obj.item_id] = [];
    }

    result[obj.item_id].push(obj);
}

now you can add the required values and push the result in new object

Upvotes: 1

Related Questions