简体   繁体   中英

ES6 map feature

I am trying out es6 map datastructure, but I when I tried to iterate the map it is giving the following error

The error occurs on line 6:
for (let [key, val] of m.entries())

SyntaxError: Unexpected token [
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:413:25)
    at Object.Module._extensions..js (module.js:452:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:475:10)
    at startup (node.js:117:18)
    at node.js:951:3

Here is my code :

"use strict"
let m = new Map()
m.set("hello", 42)
m.set(1, 34);
console.log(m);
for (let [key, val] of m.entries())
    console.log(key + " = " + val)

that's a little bit more handy solution to iterate:

let m = new Map();
m.set("hello", 42);
m.set(1, 34);

for (var [key, value] of m) {
    console.log(key + " = " + value);
}

I found one solution, here is code snippet that iterates over es6 map:

"use strict"
let m = new Map()
m.set("hello", 42)
m.set(1, 34);
for (let entry of m.entries())
    console.log(entry[0]+" "+entry[1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM