User Tools

Site Tools


javascript_sets

Javascript Sets

Set Data-Structure

Cleaner data-structure for common algorithms based on sets. ECMAScript 6 — syntactic sugar: reduced | traditional

let s = new Set();
s.add("hello").add("goodbye").add("hello");
s.size === 2;
s.has("hello") === true;
for (let key of s.values()) // insertion order
    console.log(key);

ECMAScript 5 — syntactic sugar: reduced | traditional

var s = {};
s["hello"] = true; s["goodbye"] = true; s["hello"] = true;
Object.keys(s).length === 2;
s["hello"] === true;
for (var key in s) // arbitrary order
    if (s.hasOwnProperty(key))
        console.log(s[key]);

Also see https://alligator.io/js/sets-introduction/

javascript_sets.txt · Last modified: 2018/09/10 21:45 by 119.94.96.186