Sobes.tech
Middle

What array traversal methods do you know?

sobes.tech AI

Answer from AI

Main array iteration methods in JavaScript:

  • forEach(callback) — executes a function for each element, does not return a new array.
  • map(callback) — creates a new array with elements that are the result of calling callback on each element.
  • filter(callback) — creates a new array with elements for which callback returns true.
  • reduce(callback, initialValue) — reduces the array to a single value by accumulating the result.
  • some(callback) — returns true if at least one element satisfies the condition.
  • every(callback) — returns true if all elements satisfy the condition.
  • find(callback) — returns the first element that satisfies the condition.
  • findIndex(callback) — returns the index of the first element that satisfies the condition.

Example of using map and filter:

const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2); // [2, 4, 6, 8, 10]
const evens = numbers.filter(n => n % 2 === 0); // [2, 4]

These methods allow for convenient and declarative work with arrays.

What array traversal methods do you know? — Frontend - sobes.tech