Hector
Hector

Reputation: 668

Javascript convert set to plain Array

Set - Javascript | MDN

Example: Iterating Sets

line 16

var myArr = [v for (v of mySet)];

my code:

var s_priceCatsArr = [ n for ( n of s_priceCats ) ];

This produces the error Uncaught SyntaxError: Unexpected token for on Google Chrome Version 38.0.2125.111 m

Customise and control Google Chrome > Settings > About tells me that my Google Chrome is up to date.

Am I doing something wrong or is this feature not supported?

UPDATE:

I went to chrome://flags and ticked Enable Experimental JavaScript. Then restarted my browser, but I still get the same error. I guess I'll just have to wait until that feature is added properly then... :(

Upvotes: 0

Views: 942

Answers (1)

Alnitak
Alnitak

Reputation: 339776

Chrome does not yet (as of version 38.0.2125.111) support "Array comprehensions", i.e. [expr of Iterable]

The standard ES6 function to convert an Iterable into an Array is Array.from, but that's not in Chrome yet either. For reasons I haven't yet discerned I can't get the MDN shim to work on a Set. (ah, according to the docs the shim doesn't support "true iterables")

Another approach that works in Firefox but (but again, not in Chrome) is the "spread" operator ...:

> var s = new Set([1,2,3,4])
undefined
> [...s]
[1, 2, 3, 4]

EDIT in Chrome 46 (and possibly earlier) all of for (x of <Iterable>), Array.from and the ... spread operator now work.

Upvotes: 2

Related Questions