I have one multidimensional array for which I want to apply the spread operator to get one array with no duplicate values. Here is the multidimensional array:
colorArray = [
['Bead Link Layered Anklet', 'Gold, Hematite'],
['Bead Link Layered Anklet', 'Gold, Multi, Neutral'],
['Bead Link Layered Anklet', 'Gold, Multi, Neon'],
['Bead Link Layered Anklet', 'Gold, Multi, Neon'],
['Colorful Beaded Anklet', 'Fuchsia, Gold'],
['Colorful Beaded Anklet', 'Yellow'],
['Rhinestone Pave Chain Layered Anklets','Gold Theme']
]
When I flatten the array by applying a push operator, it is no longer a multidimensional array and it looks like this:
colorArray =
[
'Bead Link Layered Anklet',
'Gold, Hematite',
'Bead Link Layered Anklet',
'Gold, Multi, Neutral',
'Bead Link Layered Anklet',
'Gold, Multi, Neon',
'Bead Link Layered Anklet',
'Gold, Multi, Neon',
'Colorful Beaded Anklet',
'Fuchsia, Gold',
'Colorful Beaded Anklet',
'Yellow',
'Rhinestone Pave Chain Layered Anklets',
'Gold Theme '
]
My problem is that I can only apply the spread operator if the array is flat but I want to preserve the structure so I don’t want to flatten it. This is what I get when I apply the spread operator to the flat array:
unique =
[
'Bead Link Layered Anklet',
'Cream, Gold',
'Gold, Hematite',
'Gold, Multi, Neutral',
'Gold, Multi, Neon',
'Colorful Beaded Anklet',
'Fuchsia, Gold',
'Yellow',
'Rhinestone Pave Chain Layered Anklets',
'Gold Theme'
]
I want the result to look like this:
['Bead Link Layered Anklet', ['Gold, Hematite', 'Gold, Multi, Neutral', 'Gold, Multi, Neon'],
['Colorful Beaded Anklet', ['Fuchsia, Gold', 'Yellow'],
['Rhinestone Pave Chain Layered Anklets','Gold Theme']
This is what I used to get rid of duplicates:
unique = [...new Set(colorArray)];