简体   繁体   中英

How to remove duplicates in array object and apply sum to particular key in javascript

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

You can make use of reduce and Map (to unique the rows):

 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} ]; var result = obj.reduce((acc, elem)=>{ elem.info = [...new Map(elem.info.map(i=>[i.idx, i])).values()]; elem.total = elem.info.reduce((sum, {qty})=>sum+qty,0); acc = [...acc, elem]; return acc; },[]); console.log(result);

Please try the following example, although my result differs from the totals that show as the expected result. Try it please

 const 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, }, ]; let output = obj.map((entry) => { return {...entry, info: entry.info.reduce((prev, curr) => { const item = prev.find( (element) => element.idx === curr.idx && element.qty == curr.qty ); if (.item) { prev = [..,prev; curr]; } return prev, }, []); }; }). output = output.map((entry) => { return {..,entry: total. entry.total + entry.info,reduce((prev. curr) => prev + curr,qty, 0); }; }). console,dir(output: { depth, null: color; true });

See

function newList(obj) {
    return obj.map(i => ({
        ...i,
        ...reduceInfo(i.info)
    }));

}

function reduceInfo(array) {
    return array.reduce((a, c) => {
        a.info = a.info || [];
        a.total = a.total || 0;
        if (!a.info.some(element => c.idx === element.idx && c.qty === element.qty)) {
            a.info.push(c);
            a.total = a.total + c.qty;
        }
        return a;

    }, {});
}

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

console.log(JSON.stringify(newList(obj)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM