Search code examples
javascriptecmascript-6google-chrome-devtoolsjavascript-objects

Running es6 in debugger console


When I open a debugger console on a page, and I run:

'use strict'
const userMap = new Map();
userMap.set("1", {id: 200})
userMap.set("2", {id: 301})
userMap.set("3", {id: 102})
[...userMap].sort((a, b)=> {
  console.log(a[1], b[1])
  return a[1] - b[1]
})

..

I will get an error:

Uncaught SyntaxError: Unexpected token ...

Now, when I use a ugly setTimeout():

'use strict'
const userMap = new Map();
userMap.set("1", {id: 200})
userMap.set("2", {id: 301})
userMap.set("3", {id: 102})
setTimeout(() => {
    [...userMap].sort((a, b)=> {
      console.log(a[1], b[1])
      return a[1] - b[1]
    })
})

Now the error is not there..

I was just wondering why is this happening?

Update, this was working for me

'use strict'
const userMap = new Map();
userMap.set("1", {id: 100});
userMap.set("2", {id: 300});
userMap.set("3", {id: 111});
const sortBy = (sortMap, name) => {
  return [...sortMap.values()].sort((a, b)=> {
    return a[name] - b[name];
  });
};
const sorted = sortBy(userMap, "id");
console.log(sorted);

Solution

  • I think you're missing the semicolon at the end of the line:

    userMap.set("1", {id: 200});
    userMap.set("2", {id: 301});
    userMap.set("3", {id: 102});