for statement with index and value
Kevin Smith
zenparsing at gmail.com
Tue Jul 14 02:22:08 UTC 2015
Destructuring is here to help:
for (let [index, value] of [1, 2, 3].entries())
console.log(index + ": " + value)
The "entries" method returns an iterator of [index, value] pairs.
On Mon, Jul 13, 2015 at 10:13 PM Tingan Ho <tingan87 at gmail.com> wrote:
> Just following a discussion we had on TypeScript
> https://github.com/Microsoft/TypeScript/issues/3835
>
> In most times, I just need the value in an array, so I use a for..of loop.
> But then, I find out that I need the index too, then I need to rewrite my
> whole for loop expression. Happens all the time for me.
>
> I have to ask why there doesn't exist a `for statement` where I can get
> the index and value of an array directly? Or for that matter property and
> value of an object?
>
> `.forEach` is not ideal since you can't break, break to outer loop and
> continue cleanly. Even though you can achieve this with other array
> methods, I don't think they are as productive as a for-statement.
>
> What I'm suggesting is augmenting the current syntax for `for..of` loops.
> And support an overloading pattern so that we don't need to rewrite the
> whole for loop expression to just to get the index when we already getting
> the value.
>
> ```
> // overloads
> for (let value, index of values) { ... }
> for (let value of values) { ... }
> ```
>
> PS. Sorry if this has already discussed. Couldn't find any on Google and
> the esdiscuss.org page doesn't have any search capabilities.
> _______________________________________________
> 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/20150714/81b4ec0a/attachment.html>
More information about the es-discuss
mailing list