JavaScript implementation of Set, a data structure to store non-repeated values.
Load the script before using it.
In the example below, the script is loaded in the head of an HTML file:
<html>
<head>
...
<script src="js-set.js"></script>
...
</head>
...
</html>
To create an instance of the JS-Set and start using it, just type: var mySet = new Set();
The list of available methods:
Returns the set as an array mySet.getSet()
Returns number of elements in the Set mySet.size()
Returns the n-th object in the set mySet.getSet(3)
:
Inserts single objects into the Set
mySet.add("foo")
.
If the object is already in the Set it's not added.
Inserts an array of objects into the Set
mySet.addAll(["bar", 1, 2, 3, [4, "five"], "six"])
Objects that are already in the Set are not added.
Removes object from Set: mySet.remove("foo")
Removes an array of objects from the Set: mySet.removeAll([1, 2, "six"])
.
Returns true if key is in the set, otherwise false mySet.contains("foo")
.
Returns true if all the elements of the given array are contained in the set, otherwise false mySet.containsAll([1, 2, "six"]);
.
Empties Set: mySet.clear()
Comments represent outputs:
var mySet = new Set();
mySet.add("foo"); // ["foo"]
mySet.add("foo"); // ["foo"] not added, already in the Set
mySet.addAll(["foo", 1, 2, "bar", 3, 4]); // ["foo", 1, 2, "bar", 3, 4]
mySet.getSet(); // ["foo", 1, 2, "bar", 3, 4]
mySet.size(); // 6
mySet.get(3); // "bar"
mySet.remove("foo"); // [1, 2, "bar", 3, 4]
mySet.removeAll([1, "bar", 2]); // [3, 4]
mySet.contains("bar"); // false
mySet.contains(4); // true
mySet.containsAll(["bar", 4]); // false
mySet.containsAll([4, 3]); // true
// Method chaining
mySet.add(1).add(2).remove(2).addAll([3, 4, 5]).getSet(); // [1, 3, 4, 5]
mySet.clear(); // []
Alejandro Perez Martin: https://www.linkedin.com/in/aleperez92