I am working in a ReactJS application where I retrieve a JSON with some products, and I need to restructure this information in order to categorize and show the information.
I have already tried with filters and maps, but it didn't turn out as expected.
[
{
"barcode": "8434786768366",
"collection": "AW",
"colorCode": "413",
"colorName": "LIGHT WINE",
"familyCode": "1",
"familyName": "T-Shirts",
"genericProduct": "PM506471",
"id": 263917,
"productCode": "PM506471413S",
"productName": "T-SHIRT",
"productNameLang": "en",
"season": "2019",
"size": "S",
"subfamilyName": "SS T-Shirts"
},
{
"barcode": "8434786768342",
"collection": "AW",
"colorCode": "413",
"colorName": "LIGHT WINE",
"familyCode": "1",
"familyName": "T-Shirts",
"genericProduct": "PM506471",
"id": 263921,
"productCode": "PM506471413L",
"productName": "T-SHIRT",
"productNameLang": "en",
"season": "2019",
"size": "L",
"subfamilyName": "SS T-Shirts"
}
]
This is a sample of what im working with, though the original array is much bigger. I need to be able to find all the objects that have the same "genericProduct" and merge them together in a single object, in order to remove duplicates. However, I need to preserve the information instead of overriding it.
Following the example above, those two objects should merge into something like this:
[
{
"barcode": ["8434786768366", "8434786768342"],
"collection": "AW",
"colorCode": "413",
"colorName": "LIGHT WINE",
"familyCode": "1",
"familyName": "T-Shirts",
"genericProduct": "PM506471",
"id": [263917, 263917],
"productCode": ["PM506471413S","PM506471413L"],
"productName": "T-SHIRT",
"productNameLang": "en",
"season": "2019",
"size": ["S","L"],
"subfamilyName": "SS T-Shirts"
}
]
As a tl;dr I need to iterate in an object array to see which have the same "genericProduct" key, and then check each property to see if it is different. If it is, I need to transform said property into an array that stores all values, being two or more.