/    Sign up×
Community /Pin to ProfileBookmark

Not able to apply the spread operator on a multidimensional array

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)];

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@makamo66authorApr 13.2020 — Correction: 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']]
Copy linkTweet thisAlerts:
@makamo66authorApr 14.2020 — The answer was posted at https://stackoverflow.com/questions/61197391/not-able-to-apply-the-spread-operator-on-a-multidimensional-array/61200822#61200822
×

Success!

Help @makamo66 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...