for statement with index and value
Logan Smyth
loganfsmyth at gmail.com
Tue Jul 14 03:52:43 UTC 2015
Unfortunately we can't have both
```
for (let value of values){
```
and
```
for (let [index, value] of values){
```
Over all, the first one is the more likely one on a day-to-day basis.
The `[]` are needed because the `for...of` follows the standard rules for
assignment, so it uses standard destructuring, and JS array destructuring
requires `[]`.
```
for (let [index, value] of values.entries()){
```
is essentially is the same as
```
for (let pair of values.entries()){
let [index, value] = pair;
```
As for your last question, `.entries` returns an iterator, so it will not
create a copy of the array.
On Mon, Jul 13, 2015 at 7:43 PM, Tingan Ho <tingan87 at gmail.com> wrote:
> >for (let [index, value] of [1, 2, 3].entries())
> console.log(index + ": " + value)
>
> I still think most people will write:
>
> ```
> for (let value of values) { ... }
> ```
> and then rewrite the whole expression inside the `for-loop` when they find
> out that they need the index too:
> ```
> for (let [index, value] of [1, 2, 3].entries())
> console.log(index + ": " + value)
> ```
> `for (let value, index of values) { ... }` is still much easier to type
> than `for (let [index, value] of [1, 2, 3].entries())` and also more
> readable.
>
>
> Also, doesn't that makes a copy of the `[1, 2, 3]`?
>
> --
> Sincerely,
>
> Tingan Ho
> @tingan87 <https://twitter.com/tingan87>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150713/e2713960/attachment.html>
More information about the es-discuss
mailing list