Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

For-Each-over an array in javascript

How might I loop through every one of the entries in an array utilizing JavaScript?

I thought it was something like this
forEach(instance in theArray)

Where theArray is my array, but this seems to be incorrect.
by

2 Answers

MounikaDasa
In such cases you can use an iterator explicitly.
Sometimes, you might want to use an iterator explicitly. You can do that, too, although it's a lot clunkier than for-of. It looks like this:

const a = ["a", "b", "c"];
const it = a.values();
let entry;
while (!(entry = it.next()).done) {
console.log(entry.value);
}
An iterator is an object matching the Iterator definition in the specification. Its next method returns a new result object each time you call it. The result object has a property, done, telling us whether it's done, and a property value with the value for that iteration. (done is optional if it would be false, value is optional if it would be undefined.)

The meaning of value varies depending on the iterator; arrays support (at least) three functions that return iterators:

values(): This is the one I used above. It returns an iterator where each value is the array entry for that iteration ("a", "b", and "c" in the example earlier).
keys(): Returns an iterator where each value is the key for that iteration (so for our a above, that would be "0", then "1", then "2").
entries(): Returns an iterator where each value is an array in the form [key, value] for that iteration
kshitijrana14
If you’re using the jQuery library, you can use jQuery.each:
$.each(yourArray, function(index, value) {
// do your stuff here
});

EDIT :
As per question, user want code in javascript instead of jquery so the edit is
var length = yourArray.length;   
for (var i = 0; i < length; i++) {
// Do something with yourArray[i].
}

Login / Signup to Answer the Question.