User Tools

Site Tools


javascript_maps

from http://es6-features.org/#MapDataStructure

Map Data-Structure

Cleaner data-structure for common algorithms based on maps.

ECMAScript 6 — syntactic sugar

let m = new Map();
let s = Symbol();
m.set("hello", 42);
m.set(s, 34);
m.get(s) === 34;
m.size === 2;
for (let [ key, val ] of m.entries())
    console.log(key + " = " + val);

ECMAScript 5 — syntactic sugar

var m = {};
// no equivalent in ES5
m["hello"] = 42;
// no equivalent in ES5
// no equivalent in ES5
Object.keys(m).length === 2;
for (key in m) {
    if (m.hasOwnProperty(key)) {
        var val = m[key];
        console.log(key + " = " + val);
    }
}

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

javascript_maps.txt · Last modified: 2018/09/10 21:50 by 119.94.96.186