0

I have an array of objects in TypeScript which have the same key and I want to group them in a row, for eg.:

    [{key: 'test', field1: 'abc', field2: 'xyz', price: 10.5}, 
    {key: 'test', field1: 'abc', field2: 'xyz', price: 1}, 
    {key: 'test', field1: 'abc', field2: 'xyz', price: 20.5},
    {key: 'test2', field1: 'r', field2: 'q', price: 20},
    {key: 'test2', field1: 'r', field2: 'q', price: 50}]

The result should be as follows:

[{key: 'test', field1: 'abc', field2: 'xyz', price: 32},
{key: 'test2', field1: 'r', field2: 'q', price: 70}]

Can you please help me to achieve this since I'm new to typescript?

Anaidm
  • 35
  • 8
  • 1
    there are a lot of js solutions. i think converting to typescript wont be hard for you – cmgchess Apr 04 '23 at 07:57
  • [This](https://tsplay.dev/m3B6AN) should work for your example, you simply check whether or not an item with the specific `key` is already available and based on that you either sum up the price or add the element to the result. – Cuzy Apr 04 '23 at 08:23

0 Answers0