1

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}
]
codecat
  • 145
  • 1
  • 11
  • 1
    Does this answer your question? [How to get unique values in an array](https://stackoverflow.com/questions/11246758/how-to-get-unique-values-in-an-array) – m02ph3u5 Jun 28 '20 at 13:30
  • may this help your question https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array – Raghul SK Jun 28 '20 at 13:32
  • Use `filter` or `reduce` – fiveelements Jun 28 '20 at 13:35
  • Does this answer your question? [Checking for duplicate strings in JavaScript array](https://stackoverflow.com/questions/49215358/checking-for-duplicate-strings-in-javascript-array) – Mike Ezzati Jun 28 '20 at 13:36
  • I think that the totals you show in the expected result do not add the original value of the total. Can you confirm it please. I think the totals should be 4 and 6 – Mario Jun 28 '20 at 14:00

3 Answers3

0

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);
gorak
  • 5,233
  • 1
  • 7
  • 19
0

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

Yousaf
  • 27,861
  • 6
  • 44
  • 69
Mario
  • 4,784
  • 3
  • 34
  • 50
0
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)));
Ganesh Kumar
  • 3,220
  • 1
  • 19
  • 27