I have a object obj
, in which how to remove duplicate in info
and apply the sum of quantity qty
to key total
in javascript.
How to remove duplicates in array object and apply sum to particular key in javascript.
function newList (obj){
return obj.map(i=>({
...i,
total: i.info.map(e => e.qty).reduce((prev, curr) => prev + curr, 0)
}));
}
var obj =[
{id:1, info:[{idx:1, qty: 1}, {idx:2, qty: 2},{idx:2, qty: 2}], code: "sample1", total: 1},
{id:2, info:[{idx:3, qty: 2}, {idx:4, qty: 2}], code: "sample2", total: 2}
]
Expected Output:
[
{id:1, info:[{idx:1, qty: 1}, {idx:2, qty: 2}], code: "sample1", total: 3},
{id:2, info:[{idx:3, qty: 2}, {idx:4, qty: 2}], code: "sample2", total: 4}
]