medikoo (2014-02-21T16:13:18.000Z)
Currently per spec Array.from doesn't produce perfect copies of sparse
arrays:

Array.from([1,,2]); // [1, undefined, 2]

I know it's been discussed [1] but there's not much feedback.

It doesn't seem right for a few reasons:

1. Array.from already produces sparse arrays from array-likes:

Array.from({ 0: 1, 2: 2, length: 3 }); // [1,,2]

So why it doesn't from sparse arrays?

2. Array.from can't be used as generic plain array copy producer.

In ES5 this can be achieved, with `arr.slice()`. However as in ES6 `slice`
will return object of same type as one on which it is called, so it is no
longer that straightforward, especially if we're strongly after plain array.

The only 100% bulletproof solutions we have in ES6 are either:

var plainCopy = new Array(arr.length)
arr.forEach(function (v, i) { plainCopy[i] = v; });

or other quite dirty way:

Array.from(Object.assign({ length: arr.length}, arr));

Other related question:

Why do array iterators go through not defined indexes? It seems not
consistent with other iteration methods we have since ES5, are there any
plans to use sparse iteration kinds [2] or are they defined just to reserve
eventual future use?

Thanks

-- Mariusz

[1] 
https://github.com/rwaldron/tc39-notes/blob/master/es6/2014-01/jan-28.md#arrayfrom
[2]
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-array-iterator-instances




--
View this message in context: http://mozilla.6506.n7.nabble.com/Array-from-and-sparse-arrays-tp308815.html
Sent from the Mozilla - ECMAScript 4 discussion mailing list archive at Nabble.com.
domenic at domenicdenicola.com (2014-03-02T22:39:14.116Z)
Currently per spec `Array.from` doesn't produce perfect copies of sparse
arrays:

```js
Array.from([1,,2]); // [1, undefined, 2]
```

I know [it's been discussed][1] but there's not much feedback.

It doesn't seem right for a few reasons:

1. `Array.from` already produces sparse arrays from array-likes:

   ```js
   Array.from({ 0: 1, 2: 2, length: 3 }); // [1,,2]
   ```

   So why it doesn't from sparse arrays?

2. `Array.from` can't be used as generic plain array copy producer.

In ES5 this can be achieved, with `arr.slice()`. However as in ES6 `slice`
will return object of same type as one on which it is called, so it is no
longer that straightforward, especially if we're strongly after plain array.

The only 100% bulletproof solutions we have in ES6 are either:

```js
var plainCopy = new Array(arr.length)
arr.forEach(function (v, i) { plainCopy[i] = v; });
```

or other quite dirty way:

```js
Array.from(Object.assign({ length: arr.length}, arr));
```

Other related question:

Why do array iterators go through not defined indexes? It seems not
consistent with other iteration methods we have since ES5, are there any
plans to use sparse iteration kinds [2] or are they defined just to reserve
eventual future use?

[1]: https://github.com/rwaldron/tc39-notes/blob/master/es6/2014-01/jan-28.md#arrayfrom
[2]: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-properties-of-array-iterator-instances