In JavaScript, the array data type consists of a list of elements. JavaScript developers can work with arrays using a variety of built-in methods. In previous articles, we covered the most commonly used javascript array methods. In this article, we will cover important javascript array methods that every developer should be aware of.
Map ( )
The map() method performs a specified function on each element of an array and returns a new array. The map calls a provided callback function once for each element in an array in the specified order and then creates a new array from the results. Only array indexes with assigned values, including undefined ones, are triggered by a callback.
var array1 = [2, 4, 6, 8];
// pass a function to map
const map1 = array1.map(function(x) {
return x * 2
});
console.log(map1);
OUTPUT
[4, 16, 36, 64]
Reduce ( )
In javascript, the reduce() method applies a function to the accumulator and each element in the array from left to right to reduce it to a single output value.
const text = ["React ", "is ", "scalable"];
// function to join each string element
function joinStrings(accumulator, currentValue) {
return accumulator + currentValue;
}
let joinedString = text.reduce(joinStrings);
console.log(joinedString);
OUTPUT:
React is scalable
Array.from ( )
In javascript, the array. From ( ) method creates a new shallow copy from an array or from an array-like iterable object.
// creating a new array by using a string
let newArray = Array.from("axios");
console.log(newArray);
OUTPUT:
[ 'a',‘x’,’i’,’o’,’s’ ]
some( )
In JavaScript provided Some ( ) function is used to determine whether at least one element in the array passes the test.
// This is a test function that returns an odd number.
function isOdd(element) {
return element % 3 === 0;
}
// Array definition
let numbers = [11, 13, 12, 16, 14];
// determines whether the numbers array contains at least one odd number
console.log(numbers.some(isOdd));
OUTPUT:
true
Every( ) :
The every( ) method in Javascript helps to ensure that all elements in an array pass the test implemented by the provided function.
function isBelowThreshold(currentValue) {
return currentValue < 40;
}
var array1 = [1, 10, 19, 47, 10, 13];
console.log(array1.every(isBelowThreshold));
OUTPUT :
False
Every method calls the provided callback function once for each array element until it finds one that returns a false value. If such an element is discovered, all methods return false right away. Otherwise, every return is true if the callback returns true for all elements.
Includes( )
In javascript, the includes() method checks whether an array contains a specified element or not. The includes() method returns a Boolean value, and by the end of the operations, it returns whether it's true or false.
let languages = ["JavaScript", "React", "Vue"];
// checking whether the array contains 'Vue'
let check = languages.includes("Vue");
console.log(check);
OUTPUT:
True
flat( ) :
In Javascript, flat() method creates a new array by flattening a nested array up to the specified depth.
let data = [11, 12, [13, 14, [15, 16, [17, 18]]]];
let flattenArray = data.flat(1);
console.log(flattenArray);
OUTPUT:
[ 11, 12, 13, 14, [ 15, 16, [ 17, 18 ] ] ]
Conclusion:
As we discussed, those mentioned above are some of the essential JavaScript Array Methods a developer should know.
Also, read our article:blog.skillsafari.in/how-to-become-a-front-e..