Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data structure to store unique values in javascript

Tags:

javascript

What is the best data type to store unique values only? an array can have duplicates

[one, one, two]

and an object (? maybe wrong terminology) have unnecessary values for my current case

{one: something, two: something, three: something}

Shortly, I need something like this:

{one, two, three}

I am not sure what it is called, or if it does exist in js. Needing some enlightment.

like image 997
Mia Avatar asked Sep 02 '25 13:09

Mia


1 Answers

You mean a structure called Set, and in the current version of ECMAScript there's no such structure. It will be standarized in the next version, however it's available now in some browsers.

You can emulate set using object, but as you said that also involves unnecessary values. If you don't want to care about them, you can use a library that emulates Set, like http://collectionsjs.com/

like image 108
Kuba Jagoda Avatar answered Sep 05 '25 03:09

Kuba Jagoda