Set to Array conversions

# Angus Croll (11 years ago)

I'm trying to figure out the most painless way, given a set, to return the set's values as an array.

Possibilities:

  1. harmony wiki (harmony:iterators) suggests the following, but it is a syntax error in traceur, continuum and "node --harmony"

    let arr = [e for e of mySet];
    
  2. The ES6 standard supports the following production (i.e. expression, not var, before 'for'):

    *IterationStatement : for ( LeftHandSideExpression of AssignmentExpression) Statement*
    

    (see people.mozilla.org/~jorendorff/es6-draft.html#sec-13.6.4.2)

    which suggests I should be able to do this:

    let arr = [];
    for (arr[arr.length-1] of mySet);
    

    (I can do the equivalent with for-in) but that also errors in the above three transpilers

  3. So then I'm left with the pedestrian:

    let arr = [];
    for (e of mySet) {
      arr.push(e);
    }
    
  4. I also wondered if Array.from(mySet) would do the trick but again doesn't seem to pass muster with any of the above transpilers. (continuum returns a zero length array and the other two don't know Array.from)

Wondering if I'm missing something better.

# Claude Pache (11 years ago)

I suggest:

[ ... mySet ]

or, if you don't want to use any new syntax:

Array.from(mySet)

P.S. The syntax [e for e of mySet] is outdated in Harmony, you should use [(for let e of mySet) e].

# Angus Croll (11 years ago)

Thanks - I missed the obvious one I guess - though it still returns an empty array in continuum and traceur.

e.g

[...(new Set([1,2,3])].length; //0

or

var s = new Set();
s.add('a');
s.add('b');
[...s].length; //0

assuming they just need to catch up

# Brandon Benvie (11 years ago)

On 9/16/2013 9:22 AM, Angus Croll wrote:

Thanks - I missed the obvious one I guess - though it still returns an empty array in continuum and traceur.

At one point spread was to only work on indexed (array-like) objects. Now it works on both indexed objects as well as iterables. This works in SpiderMonkey currently.

# Brendan Eich (11 years ago)

Claude, thanks for answering, one correction at bottom:

Claude Pache <mailto:claude.pache at gmail.com> September 16, 2013 11:42 AM

P.S. The syntax [e for e of mySet] is outdated in Harmony, you should use [(for let e of mySet) e].

No, the new syntax is [for (e of mySet) e] -- no let and parens in the same place as in the for-of/in loop statements.

# Claude Pache (11 years ago)

Le 16 sept. 2013 à 18:38, Brendan Eich <brendan at mozilla.com> a écrit :

No, the new syntax is [for (e of mySet) e] -- no let and parens in the same place as in the for-of/in loop statements.

Yes, thanks. The wrong placement of parens was a typo. But for the let keyword, I just realise that there is a discrepancy between for/of loops and comprehensions.

# Rick Waldron (11 years ago)

On Mon, Sep 16, 2013 at 11:33 AM, Angus Croll <anguscroll at gmail.com> wrote:

I'm trying to figure out the most painless way, given a set, to return the set's values as an array.

set.values(); // An array of the set's values

Possibilities:

  1. harmony wiki (harmony:iterators) suggests the following, but it is a syntax error in traceur, continuum and "node --harmony"
let arr = [e for e of mySet];

Just a heads up, that wiki page is out of date (see the notice at the top of the page) and shows the old syntax (as shown above). The up-to-date syntax is:

let arr = [for (e of mySet) e];

(I realize this is beside the point of the question)

# Rick Waldron (11 years ago)

On Mon, Sep 16, 2013 at 2:59 PM, Rick Waldron <waldron.rick at gmail.com>wrote:

set.values(); // An array of the set's values

Whoops, that's totally wrong information. (that's what I get for rushing blindly into a thread!) Apologies.

# Rick Waldron (11 years ago)

Whoops, that's totally wrong information. (that's what I get for rushing blindly into a thread!) Apologies.

I think I owe some clarification and an explanation of my apparent madness. I made this mistake is because I've grown accustomed to using for-of (FirefoxOS!) for iteration:

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

for (var n of s) { ... }

for (var n of s.values()) { ... }

var a = [1,2,3,4];

for (var n of a) { ... }

As you can see, there is no need to consider the data types different when iterating their values—which is the excuse I'm going with: getting too comfortable. Anyway, apologies again for the incorrect information; the good news is that these new data types fit well in common patterns.

# Brendan Eich (11 years ago)

Claude Pache wrote:

Yes, thanks. The wrong placement of parens was a typo. But for the let keyword, I just realise that there is a discrepancy between for/of loops and comprehensions.

Quite intentionally so -- we do not want the same option to update a loop control variable bound otherwise than by let implicitly. Just to be clear!