Home javascript Analog of the foreach function for javascript

Analog of the foreach function for javascript

Author

Date

Category

Or is there another way to iterate over all the elements of an array in Javascript?

Something like foreach (array as value)


Answer 1, authority 100%

There are several options depending on your needs.

Let’s start simple:

var a = ["a", "b", "c"];
a.forEach (function (entry) {
  console.log (entry);
});

Slightly more old-fashioned option:

var index;
var a = ["a", "b", "c"];
for (index = 0; index & lt; a.length; ++ index) {
  console.log (a [index]);
}

In addition, there is actually a for-in . Can be used, for example, for sparsed arrays:

Primitive, but not entirely correct option:

for (var key in data) {
 key; // key
 data [key]; // value
}

More correct:

var key;
var a = [];
a [0] = "a";
a [10] = "b";
a [10000] = "c";
for (key in a) {
  if (a.hasOwnProperty (key) & amp; & amp;
    /^0$|^^^^\d*$/.test(key) & amp; & amp;
    key & lt; = 4294967294
    ) {
    console.log (a [key]);
  }
}

In EcmaScript6, a for-of construction has been added as a draft:

var val;
var a = ["a", "b", "c"];
for (val of a) {
  console.log (val);
}

Actually, by and large, for-of is just a correct for-in , which does not require additional checks as in the example above. Fundamental differences in details can be found in sentences .

More details:


Answer 2

In standard JavaScript, arrays have a method forEach :

['a', 'b', 'c']. forEach (function (value, index) {
  console.log ('a [' + index + '] =' + value);
});

We get:

a [0] = a
a [1] = b
a [2] = c

Not to be confused with for (key in object) . This construction iterates over object properties , not array elements. Although the elements of the array are considered properties of the array, you will get more than just them, and you will have to filter the properties with hasOwnProperty .


Answer 3

for (var i = 0; i & lt; array.length; i ++) {
  console.log (array [i]);
}

Answer 4

You can use the $ .each function https://api.jquery.com/ each /


Answer 5

for (var i = 0; i & lt; = myArray.length; i ++) {/ * do what you need here with myArray [i] * /}

Answer 6

see the implementation Array.prototype.forEach ()

Syntax

arr.forEach (callback [ thisArg])

Options

callback The function that creates an element of a new array takes three arguments:

  • currentValue The current item being processed in the array.

  • index The index of the currently processed element in the array.

  • array The array to traverse.

thisArg Optional parameter. The value to use as this when calling the callback function.

The following code prints each element of the array on a new log line:

function logArrayElements (element, index, array) {
 console.log ('a [' + index + '] =' + element);
}
// Note the skip at index 2, there is no element, so it is not visited
[2, 5, 9] .forEach (logArrayElements);
// logs:
// a [0] = 2
// a [1] = 5
// a [3] = 9

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions