Here is a semi complete map poly of a older project of mine.
'use strict'; class Map { constructor (keyVal) { this.clear(); if (Array.isArray(keyVal)) { keyVal.forEach(kv => this._keys.push(kv[0]) && this._values.push(kv[1])); } } get size () { return this._keys.length; } has (key) { return this._keys.includes(key); } get (key) { return this._values[this._keys.indexOf(key)]; } set (key, value) { let i = this._keys.indexOf(key); if (i !== -1) { this._values[i] = value; } else { this._keys.push(key); this._values.push(value); } } delete (key) { let i = this._keys.indexOf(key); if (i === -1) { return false; } this._keys.splice(i, 1); this._values.splice(i, 1); } keys () { return this._keys; } values () { return this._values; } clear () { this._keys = []; this._values = []; } entries () { return this._iterator(); } _iterator () { let i = 0; return { next: () => i < this._keys.length ? {value: [this._keys[i], this._values[i++]], done: false} : {done: true} }; } forEach (callback, scope = null) { this._keys.forEach(key => callback.call(scope, this.get(key), key, this)); } }
Amazingly close to what @Gordon already had :D
@PaddeK started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Here is a semi complete map poly of a older project of mine.
Amazingly close to what @Gordon already had :D