JS Array Helper Method

ES6 introduced powerful array iteration methods that make working with arrays simpler, cleaner, and more readable. These helper methods iterate over arrays and apply functions to their elements.


forEach:

Executes a provided function once for each array element. It does not return anything.

Example
const numbers = [1, 2, 3, 4, 5];

// Example: Logging each element
numbers.forEach((num) => {
console.log(num);
});

Try it yourself


map:

Creates a new array by applying a function to each element of the original array. It returns the transformed array.

Example
const numbers = [1, 2, 3, 4, 5];

// Example: Squaring each element
const squares = numbers.map((num) => num * num);
console.log(squares); // [1, 4, 9, 16, 25]

Try it yourself


find:

Returns the first element in the array that satisfies a specified condition. If no element matches, it returns undefined.

Example
const numbers = [1, 2, 3, 4, 5];

// Example: Finding the first even number
const firstEven = numbers.find((num) => num % 2 === 0);
console.log(firstEven); // 2

Try it yourself


findIndex:

Returns the index of the first element that satisfies a specified condition. If no element matches, it returns -1.

Example
const numbers = [1, 2, 3, 4, 5];

// Example: Finding the index of the first even number
const firstEvenIndex = numbers.findIndex((num) => num % 2 === 0);
console.log(firstEvenIndex); // 1

Try it yourself


filter:

Creates a new array containing elements that pass a specified condition (filter function). It returns the filtered array.

Example
const numbers = [1, 2, 3, 4, 5];

// Example: Filtering out even numbers
const evens = numbers.filter((num) => num % 2 === 0);
console.log(evens); // [2, 4]

Try it yourself


reduce:

Executes a reducer function on each element of the array, resulting in a single output value.

Example
const numbers = [1, 2, 3, 4, 5];

// Example: Summing up all elements
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum); // 15

Try it yourself


some:

Checks if at least one element in the array satisfies a specified condition. Returns true or false.

Example
const numbers = [1, 2, 3, 4, 5];

// Example: Checking if the array contains any even numbers
const hasEven = numbers.some((num) => num % 2 === 0);
console.log(hasEven); // true

Try it yourself


every:

Checks if all elements in the array satisfy a specified condition. Returns true or false.

Example
const numbers = [1, 2, 3, 4, 5];

// Example: Checking if all elements are positive
const allPositive = numbers.every((num) => num > 0);
console.log(allPositive); // true

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.