Home > Back-end >  How to get duplicate values and generate another another array in javascript?
How to get duplicate values and generate another another array in javascript?

Time:12-12

I have been stuck in one situation where I need data for my easy invoice. I am using using in node.js. I want to make wrap duplicate values in one with sum of quantities.

I have products array

`[
  {
    quantity: 1,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 1,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 1,
    description: 'Angular crash course',
    'tax-rate': -40.5,
    price: 35
  },
  {
    quantity: 1,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 1,
    description: 'Angular crash course',
    'tax-rate': -40.5,
    price: 35
  },
  {
    quantity: 1,
    description: 'PHP Training',
    'tax-rate': -40.5,
    price: 35
  }
]`

Now I want a desired output to be created for my invoice like

`[
  {
    quantity: 3,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 2,
    description: 'Angular crash course',
    'tax-rate': -40.5,
    price: 35
  },
  {
    quantity: 1,
    description: 'PHP Training',
    'tax-rate': -40.5,
    price: 35
  }
]`

I have used several functional but could not get my desired output. Please provide me a solution I will be thankful and every answer will be highly appreciated. Thanks.

I have tried using forEach but could not get desired output.

CodePudding user response:

const input = [
  {
    quantity: 1,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 1,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 1,
    description: 'Angular crash course',
    'tax-rate': -40.5,
    price: 35
  },
  {
    quantity: 1,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 1,
    description: 'Angular crash course',
    'tax-rate': -40.5,
    price: 35
  },
  {
    quantity: 1,
    description: 'PHP Training',
    'tax-rate': -40.5,
    price: 35
  }
];

const output = input.reduce((map, item) => {
  if (map[item.description])
    map[item.description]["quantity"]  ;
  else 
    map[item.description] = item;
  return map  
}, {});

console.log(Object.values(output));

CodePudding user response:

Not going to comment on the data structure, but here is one way of doing it and you can certainly improve on it.

let data = [
  {
    quantity: 1,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 1,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 1,
    description: 'Angular crash course',
    'tax-rate': -40.5,
    price: 35
  },
  {
    quantity: 1,
    description: 'Node Training Package',
    'tax-rate': -40.5,
    price: 20
  },
  {
    quantity: 1,
    description: 'Angular crash course',
    'tax-rate': -40.5,
    price: 35
  },
  {
    quantity: 1,
    description: 'PHP Training',
    'tax-rate': -40.5,
    price: 35
  }
]

let temp = {};

for(let i=0;i < data.length; i  ) {
  if(temp[data[i].description] == null) {
    temp[data[i].description] = data[i];
  } else {
    temp[data[i].description]["quantity"]  = data[i]["quantity"];
  }
}

let result = Array(temp.length);

let keys = Object.keys(temp);
for(let i=0; i < keys.length; i  ) {
  result[i] = temp[keys[i]];
}

console.log(result);

  • Related