I'm trying to merge an array of objects after a condition is true. I tried to use the reducer function, but I'm not sure that is the right way.
This is my array of objects:
[
{
"Profile number": "S12",
"Line Number": "5874",
"Name": "Pillow",
"Color": "White",
},
{
"Profile number": "S12",
"Line Number": "5874",
"Name": "Blanket",
"Color": "Blue",
},
{
"Profile number": "S12",
"Line Number": "5874",
"Name": "Pillowcase",
"Color": "White",
},
{
"Profile number": "S41",
"Line Number": "8730",
"Name": "Curtain",
"Color": "White",
}
]
What I want to do here, is that if the Profile number is the same, they should merge like this:
[
{
"Profile number": "S12",
"Line Number": "5874",
"Name": "Pillow",
"Color": "White",
"Name2": "Blanket",
"Color2": "Blue",
"Name3": "Pillowcase",
"Color3": "White",
},
{
"Profile number": "S41",
"Line Number": "8730",
"Name": "Curtain",
"Color": "White",
}
]
How I should approach this?
Many Thanks,