'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
var shipTo = [
{ addressLine1: "address 1", name: "Jeff" },
{ addressLine1: "address 2", name: "Taylor" },
{ addressLine1: "address 1", name: "Megan" },
{ addressLine1: "address 3", name: "Madison" },
];
Grouped function
function groupArrayOfObjects(list, key) {
return list.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
}
I have groupedBy address attrube by this function above by address
const groupedAddress = groupArrayOfObjects(shipTo, "addressLine1");
My new data-structure is
My final Result wanna be like this :
Shipping Address for Shipment 1 & 3 is "address 1"
Shipping Address for Shipment 2 is "address 2"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

