Array Helper Methods in ES6
Last Updated :
29 Jul, 2024
Array helper methods in ES6 (JavaScript) are extremely useful for working with data stored in arrays. As a web developer, you often work with arrays, whether they contain simple numbers or complex objects with many attributes. These methods significantly simplify the manipulation of arrays, making it easier to perform complex tasks and handle intricate arrays of objects.
There are several array methods available in JavaScript ES6 which are as follows:
Using forEach() method
The forEach() method is used to iterate over all the entries of the array.
Example: To demonstrate finding the sum of all the numbers in an array of numbers.
javascript
let sum = 0;
const numbers = [1, 2, 3, 4, 5];
numbers.forEach((num) => {
sum = sum + num;
});
console.log(sum);
Using map() method
The map() method is used to iterate through all the entries of the array, modifies them and returns a new array but the old array is not manipulated.
Example: To demonstrate multiplying each number of the array by 2 and generating a new array.
javascript
const numbers = [1, 2, 3, 4, 5];
let double = numbers.map((num) => {
// Its mandatory to have a
// return while using map
return num * 2;
});
console.log(double);
Example: The map() method can also be used to extract attributes from an array of objects and store it in another array.
javascript
const objects = [
{ a: 1, b: 2 },
{ a: 3, b: 4 },
{ a: 5, b: 6 }
];
let onlybs = objects.map((object) => {
return object.b;
});
console.log(onlybs);
Using filter() method
The filter() method is used to iterate through all the entries of the array and filters out required entities into another array.
Example: To demonstrate filtering all objects where flag is 1.
javascript
let objects = [
{ flag: 1, a: 1 },
{ flag: 0, a: 2 },
{ flag: 1, a: 3 }
];
objects.filter((object) => {
if (object.flag === 1) {
// Display the result
console.log("flag:" + object.flag
+ ", a:" + object.a);
}
});
Outputflag:1, a:1
flag:1, a:3
Using find() method
The find() method is used to iterate through all the entries of the array and returns the record matching with a particular condition.
Example: To demonstrate finding an object where flag is 0.
javascript
let objects = [
{ flag: 1, a: 1 },
{ flag: 0, a: 2 },
{ flag: 1, a: 3 }
];
// Use find() function to
// filter the object
objects.find((object) => {
if (object.flag === 0) {
// Display the result
console.log("flag:" + object.flag
+ ", a:" + object.a);
}
});
Similar Reads
Best-Known JavaScript Array Methods An array is a special variable in all programming languages used to store multiple elements. JavaScript array come with built-in methods that every developer should know how to use. These methods help in adding, removing, iterating, or manipulating data as per requirements.There are some Basic JavaS
6 min read
How to clone array in ES6 ? The spread operator in ES6 is used to clone an array, whereas the slice() method in JavaScript is an older way that provides 0 as the first argument. These methods create a new, independent array and copy all the elements of oldArray to the new one i.e. both these methods do a shallow copy of the or
2 min read
JavaScript Helper Methods An array in JavaScript is usually considered a "list-objects". In simple words, we can say an array is an object that contains some values. But an array is a special object in JavaScript. An array can store heterogeneous data structures. It can store data values of any type like objects and array. E
15+ min read
JavaScript Array entries() Method The entries() method in JavaScript is used to create an iterator that returns key/value pairs for each index in the array.It allows iterating over arrays and accessing both the index and value of each element sequentially.Syntax:array.entries()Parameters:This method does not accept any parameters.Re
3 min read
JavaScript Array some() Method The some() method checks if any array elements pass a test provided as a callback function, returning true if any do and false if none do. It does not execute the function for empty elements or alter the original array.Syntaxarr.some(callback(element,index,array),thisArg);Parameterscallback: This pa
3 min read