How many ways we can iterate the objects and arrays in javascript (ES6)?
For further actions, you may consider blocking this person and/or reporting abuse
How many ways we can iterate the objects and arrays in javascript (ES6)?
For further actions, you may consider blocking this person and/or reporting abuse
Can you help us make DEV a better place?
Fill out this survey and help us by becoming a tag moderator here at DEV.
Mihir Chhatre -
Saurabh Dashora -
Said Mounaim -
BekahHW -
Once suspended, 10secondsofcode will not be able to comment or publish posts until their suspension is removed.
Once unsuspended, 10secondsofcode will be able to comment and publish posts again.
Once unpublished, all posts by 10secondsofcode will become hidden and only accessible to themselves.
If 10secondsofcode is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to Elango Sundar.
They can still re-publish the post if they are not suspended.
Thanks for keeping DEV Community 👩💻👨💻 safe. Here is what you can do to flag 10secondsofcode:
Unflagging 10secondsofcode will restore default visibility to their posts.
Top comments (10)
I guess you could add a few more...
So long as you add return:
You will be fine in Apple browsers :D
I knew I forgot something :-)
In any case, you won't be fine if the lenght of the array exceeds the one of the call stack.
Nuh-uh, bronathan! In fact, I assume that the logic / base case of the handling is void-returning.
The
return
here is purely to opt in to tail call optimization, which means you won't run out of stack :)Once again, only implemented on Apple browsers at the moment, and probably staying that way.
Your misunderstanding might be resolved if you take the following as an example:
An extreme example, I concede, but as an exercise, you can try to figure out the call stack sizes for different JS engines (and AFAIK they're all smaller than the maximum number the 52bit mantissa of a Number can store).
Optimized tail calls don't grow the stack.
They're implemented with a goto, it's basically a loop.
On most browsers this will give you a number.
On Safari, Mobile Safari, and some embeddable runtimes like latest Duktape It will be an infinite loop.
@alex Nicely explained...Thanks..!
I didn't explain much, just wrote down the first few I could think of. I didn't even say why you should avoid recursion (you might overflow the call stack).
I'd be more wary of someone that does #5 :)
Or 2 or 3 for side effects.
Here's one more that doesn't overflow stack :)
It's full of dumb hacks though, in real life I'd do this:
Why only
0x80
? To have a lot of headroom, in case thefn
is also something recursive.The real problem here is that a
1024**3
array will quickly use up your memory, even if you start filling it with0
,undefined
or even pointers to the same object. It can only exist as sparse.