Skip to content
Advertisement

How to groupBy an Array of Objects and convert the duplicates

Hello to all i have this data structure that i need to group and if there are any duplicates to convert those two objects it in one line..

Data Structure

JavaScript

Grouped function

JavaScript

I have groupedBy address attrube by this function above by address

JavaScript

My new data-structure is

enter image description here

My final Result wanna be like this :

Shipping Address for Shipment 1 & 3 is "address 1"

Shipping Address for Shipment 2 is "address 2"

Advertisement

Answer

reduce can take the index of the current array element as the 3rd parameter. you can then pass it to your accumulator and in the final array can do what you want with it

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement