Let's say I have
myArray = ['item1', 'item2']
I tried
for (var item in myArray) {console.log(item)}
It prints 0 1
What I wish is to have item1 item2
Is there any other syntax that works without using
for (var i = 0; i < myArray.length; i++)
forEach() is an array function from Node. js that is used to iterate over items in a given array. Parameter: This function takes a function (which is to be executed) as a parameter. Return type: The function returns array element after iteration.
forEach() methods provide a simpler way to iterate over arrays and NodeLists while still having access to the index. You pass a callback function into the forEach() method. The callback itself accepts three arguments: the current item in the loop, the index of the current item in the loop, and the array itself.
You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run.
Iterating over an array You can iterate over an array using for loop or forEach loop. Using the for loop − Instead on printing element by element, you can iterate the index using for loop starting from 0 to length of the array (ArrayName. length) and access elements at each index.
You can use Array.forEach
var myArray = ['1','2',3,4] myArray.forEach(function(value){ console.log(value); });
What you probably want is for...of
, a relatively new construct built for the express purpose of enumerating the values of iterable objects:
let myArray = ["a","b","c","d"]; for (let item of myArray) { console.log(item); }
... as distinct from for...in
, which enumerates property names (presumably1 numeric indices in the case of arrays). Your loop displayed unexpected results because you didn't use the property names to get the corresponding values via bracket notation... but you could have:
let myArray = ["a","b","c","d"]; for (let key in myArray) { let value = myArray[key]; // get the value by key console.log("key: %o, value: %o", key, value); }
1Unfortunately, someone may have added enumerable properties to the array or its prototype chain which are not numeric indices... or they may have assigned an index leaving unassigned indices in the interim range. The issues are explained pretty well here. The main takeaway is that it's best to loop explicitly from 0
to array.length - 1
rather than using for...in
.
So, this is not (as I'd originally thought) an academic question, i.e.:
Without regard for practicality, is it possible to avoid
length
when iterating over an array?
According to your comment (emphasis mine):
[...] why do I need to calculate the size of an array whereas the interpreter can know it.
You have a misguided aversion to Array.length
. It's not calculated on the fly; it's updated whenever the length of the array changes. You're not going to see performance gains by avoiding it (apart from caching the array length rather than accessing the property):
Now, even if you did get some marginal performance increase, I doubt it would be enough to justify the risk of dealing with the aforementioned issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With