As I know in python i can set a list to a unique list like:
In [12]: a=range(12)
In [13]: a.append(5)
In [14]: a.append(4)
In [15]: a.append(5)
In [16]: a
Out[16]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 5, 4, 5]
In [17]: set(a)
Out[17]: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
it is very useful for some scene, i just want to know how to this in javascript,
As I know in python i can set a list to a unique list like:
In [12]: a=range(12)
In [13]: a.append(5)
In [14]: a.append(4)
In [15]: a.append(5)
In [16]: a
Out[16]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 5, 4, 5]
In [17]: set(a)
Out[17]: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
it is very useful for some scene, i just want to know how to this in javascript,
Share Improve this question asked Dec 21, 2011 at 5:41 timgertimger 9623 gold badges13 silver badges32 bronze badges 2- I recommend very highly that you search Google before you ask questions here. I Googled it and found that there is no native 'Set' in Javascript. It will be simple to implement your own Set data-structure. – Zéychin Commented Dec 21, 2011 at 5:45
- The question was at the top of google when I searched. – nu everest Commented Sep 20, 2016 at 21:16
5 Answers
Reset to default 12Javascript does not have the notion of sets. However, you can use a side effect of the fact that objects cannot have duplicate property names to replicate the functionality of a set, as seen on this blog article about the topic.
From the article:
var your_array = ['a', 'a', 'a', 'b', 'b'],
set = {};
for (var i = 0; i < your_array.length; i++)
set[your_array[i]] = true;
list = [];
for (var item in set)
list.push(item);
EDIT in 2017: This is no longer true, JS got set support! MDN Docs
empty_set = new Set()
three_element_set = new Set([1, 1, 1, 2, 2, 3])
three_element_set.add(3) // still three element set
No, there isn't. JS is pretty basic, you have to either do it yourself or find a library where someone else has already done it.
The standard way to do this is usually insert elements into a hash, then collect the keys - since keys are guaranteed to be unique. Or, similarly, but preserving order:
function uniq(arr) {
var seen = {}, result = [];
var len = arr.len;
for (var i = 0; i < len; i++) {
var el = arr[i];
if (!seen[el]) {
seen[el] = true;
result.push(el);
}
}
return result;
}
Create a Set object with the Set() constructor. The argument to the Set() constructor need not be an array: any iterable object (including other Set objects) is allowed.
let myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 5, 4, 5]
let mySet = new Set(myArray); //output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
Another example:
let unique = new Set("Mississippi"); //output: [ "M", "i", "s", "p" ]
ref. JavaScript_ The Definitive Guide, David Flanagan -(2020)
Firefox 13+ provides an experimental implementation of sets. link.
Very late to the party, but as of ECMAScript 2015 the "experimental implementation" mentioned in mquandalle's answer is no longer experimental and is instead part of the standard. Browser compatibility