Next.js Discord

Discord Forum

can't fix duplicates in array

Answered
Brown bear posted this in #help-forum
Open in Discord
Original message was deleted.
Answered by American Crow
Use a Set instead of an Array. It's a data type which does not allow duplicates
View full answer

25 Replies

in strict mode IIRC, the useEffect runs two times
so the data is populated first time
but then again data is getting added to the array
would you explain what you're trying to do? to explain the answer better
Brown bear
i am trying to get all different eventUpdates from the function getEventUpdates
so if there are duplicates they wouldnt be added to the array
so you get like new update of that event everytime the eventId is changed?
Brown bear
no i just get a few same objects everytime it is called, but somewhy the function gets called two times, so there are duplicates
you can disable strict mode if you want?
Brown bear
^ to disable in next.js
^ in good old react
Brown bear
removing duplicates is more reliable, because changing strict mode could couse my other functions to break
noice
I saw somewhere, that strict mode should not cause issues for our code
IDK
Brown bear
ill try to fix by removing duplicates, this code almost seems right, just a small mistake somewhere here
yeah, good luck
American Crow
Use a Set instead of an Array. It's a data type which does not allow duplicates
Answer
Brown bear
i need to keep the order of the items
American Crow
Sets are ordered
Brown bear
i get an error trying to map the set, can you map them??