Home:ALL Converter>Javascript iterate over derived array

Javascript iterate over derived array

Ask Time:2012-02-11T00:10:59         Author:Explosion Pills

Json Formatter

I'm pretty shocked I can't find an answer to this (maybe there isn't one). Suppose I want to iterate over an array one time. I don't to have to create a variable to iterate with, but it seems that that's what I have to do. Ideally, I'd be able to do something like this:

for (key, val in 'one two three'.split(' ')) {
   console.log(val);
}

However, it seems that you can only get the keys from javascript's for..in syntax. Is there any way to iterate over that array without storing it to a variable first?

Author:Explosion Pills,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9230875/javascript-iterate-over-derived-array
T.J. Crowder :

About half of all web surfers (as of this writing, the number's going up all the time) are using browsers that natively support ECMAScript5 (e.g., the 5th edition), which has some new constructs for this, including forEach:\n\n\"one two three\".split(\" \").forEach(function(val, key) {\n // do stuff here\n});\n\n\n(Or if you don't need the key variable, just leave it off [e.g., ...forEach(function(val) {...].)\n\nEven if the browser doesn't have it, forEach is one of those features that can be \"shimmed\" (emulated) entirely correctly even in older browsers, e.g. via es5_shim.js or similar. So just include the shim (or write your own) and happily use it regardless of whether it's natively supported or something you've added. Or you may be using a library that already provides it or an analogue of it (jQuery has $.each, Prototype adds each to arrays, Backbone offers something similar, etc.).\n\nforEach also has the advantage that the loop-specific variables (entry and index) are contained by the iterator function and so you don't pollute the scope in which you're using it. There is a runtime cost, because you have the function call overhead for every array element. But function calls are very fast in real terms (even on IE6, which is a dog), you can be sure that whatever else you're doing in the loop body will completely wash out the added overhead.\n\nWith the \"older\" version of JavaScript (3rd edition), there's nothing built in, you either provide your own function for doing it, or on every loop you declare a variable for the array (as well as an indexing variable and an array entry variable), e.g.:\n\nvar array = \"one two three\".split(\" \");\nvar key, val;\nfor (key = 0; key < array.length; ++key) {\n val = array[key];\n // do stuff here\n}\n\n\nBut again you could provide your own forEach easily enough.\n\nAnother construct you can use both in older and newer engines is for..in, which iterates over the enumerable properties of an object. You still end up needing all of the variables, though. Because that means more than just \"indexes\", you have to add some safeguards:\n\nvar array = \"one two three\".split(\" \");\nvar key, val;\nfor (key in array) {\n if (array.hasOwnProperty(key) && String(Number(key)) === key) {\n // It's a numeric key and the object itself owns it\n val = array[key];\n // do stuff here\n }\n}\n\n\n...but in this case it doesn't buy you anything. It's useful for when the array is sparse (e.g., has large gaps in it).",
2012-02-10T16:13:27
yy