0

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.

GKC
  • 23
  • 6
  • seems like `description` is the unique identifier here (bad key see if you have a unique ID in records and use that instead), use it as a key and create a map and count as you are generating the map. – Shub Dec 12 '22 at 05:16
  • actually the problem is I am getting data like that only as bookings are getting save with JavaScript array randomly with multidimensional array. hence i need to get data based on description. – GKC Dec 12 '22 at 05:21

2 Answers2

0

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));
Amir MB
  • 3,233
  • 2
  • 10
  • 16
0

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);
Matthew
  • 3,136
  • 3
  • 18
  • 34