0

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... :(

Hector
  • 659
  • 1
  • 11
  • 25
  • As is **clearly** stated at the top of the documentation you linked, that's an ES6 feature. Chrome generally doesn't support those as soon as Firefox does. – Pointy Nov 07 '14 at 13:33
  • @Pointy: But Kangax's table says Chrome 38 *does* support sets: http://kangax.github.io/compat-table/es6/#Set No idea about that specific syntax, the note on that says it doesn't have support for the constructors yet. – T.J. Crowder Nov 07 '14 at 13:34
  • You can try looking at `chrome://flags` and select the "Enable experimental JavaScript" flag. That's also mentioned in the page you linked! – Pointy Nov 07 '14 at 13:34
  • 2
    Chrome doesn't support array comprehensions yet. It does support (via that flag) the `Set` and `Map` APIs. I was testing this just a day or two ago. – Alnitak Nov 07 '14 at 13:35
  • @T.J.Crowder well I guess Set support and comprehension support are two different things? – Pointy Nov 07 '14 at 13:36
  • @Pointy: Indeed. I couldn't tell whether that `[1]` referred just to OP25 or to CH38 *and* OP 25... :-) Re comprehensions, yeah, that's what I meant about syntax above. – T.J. Crowder Nov 07 '14 at 13:36
  • @Alnitak: [No active development](https://www.chromestatus.com/feature/4990451081805824) :( – georg Nov 07 '14 at 13:49
  • 1
    @georg the final comment suggests that as a language feature it has been pushed back to ES7. The `Array.from` version is more powerful anyway, if a little more verbose. – Alnitak Nov 07 '14 at 13:51
  • Possible duplicate of [How to convert a Set to an Array in Chrome?](http://stackoverflow.com/questions/29638234/how-to-convert-a-set-to-an-array-in-chrome) – Eric Oct 27 '15 at 21:47

1 Answers1

2

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.

Alnitak
  • 334,560
  • 70
  • 407
  • 495