7 examples of 'js new map' in JavaScript

Every line of 'js new map' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
95export default function Map() {
96 let keys = [];
97 let values = [];
98
99 this.get = function getValueForKey(key) {
100 const index = keys.indexOf(key);
101 if (index === -1) {
102 return undefined;
103 }
104 return values[index];
105 };
106
107 this.set = function setValueForKey(key, value) {
108 const index = keys.indexOf(key);
109 if (index === -1) {
110 keys.push(key);
111 values.push(value);
112 } else {
113 values[index] = value;
114 }
115 };
116
117 this.delete = function deleteKey(key) {
118 const index = keys.indexOf(key);
119 if (index !== -1) {
120 keys.splice(index, 1);
121 values.splice(index, 1);
122 }
123 };
124
125 this.clear = function clear() {
126 keys = [];
127 values = [];
128 };
129
130 this.forEach = function(callback, context) {
131 for (let i = 0; i < keys.length; i++) {
132 if (context) {
133 callback.call(context, values[i], keys[i]);
134 } else {
135 callback(values[i], keys[i]);
136 }
137 }
138 };
139}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

73export function map(map: MapFunc) { return new Map(map); }
93function createMap() {
94 if( Map !== void 0 ) {
95 return new Map();
96 }
97 else {
98 return new ObjectMap();
99 }
100}
7export function create<K, V>(v?: Array<[K, V]>): Map<K, V> {
8 return new Map(v);
9}
77function createMap(): UpdatersMap {
78 if (typeof Map !== "undefined") {
79 return new Map<number, Function>();
80 }
81
82 const poorMap: Record<number, Function> = {};
83
84 // ponyfill for Map
85 return {
86 set(id: number, updater: Function) {
87 poorMap[id] = updater;
88 },
89 delete(id: number) {
90 delete poorMap[id];
91 },
92 forEach(cb: (updater: Function) => void) {
93 for (const id in poorMap) {
94 cb(poorMap[id]);
95 }
96 },
97 };
98}
17export function createMap<T>(): MapLike<T> {
18
19 let obj: any = Object.create(null);
20 obj.__v8__ = undefined;
21 delete obj.__v8__;
22 return obj;
23
24}
103export function createMapProxy(map) {
104 return new Proxy(map, {
105 get(obj, prop) {
106 if (map.map.has(prop)) {
107 return map.get(prop);
108 }
109 return map[prop];
110 },
111 set(obj, prop, val) {
112 if (typeof val !== 'function') {
113 map.map.set(prop, val);
114 }
115 return true;
116 },
117 });
118}

Related snippets