Arrays are one of the most used, and one of the most important data structures. All kinds of programming languages provide different ways to work with arrays. I have often seen students and early adopters of Javascript writing long functions, iterating over the array to solve some trivial problems. Javascript, with the kind of community support it has, provides a rich set of functions to work with arrays. In this post, we will read about a few Javascript Array Functions.
-
-
array.every()
Every()
method lets you check if all the elements of an array satisfy given criteria. The criteria are passed to the method as a function that returns true or false. When called, the method runs the function once for each element of the array. If the function returns false for any element, the method returnsFalse
, else it returnsTrue
const isPositive = (number) => { return number >= 0; } const numbers1 = [1,2,3,4,5]; const numbers2 = [1,2,-3,4,5]; console.log(numbers1.every(isPositive)); // True console.log(numbers2.every(isPositive)); // False
-
array.some()
Some()
method lets you check if any of the elements of an array satisfies the given criteria. The criteria are again passed to the method as a function that returns true or false. When called the method runs the function once for each element of the array. If the function returns true for any element, the method returnsTrue
and stops execution, else it returnsFalse
const isPositive = (number) => { return number >= 0; } const numbers1 = [-1,-2,-3,4,5]; const numbers2 = [-1,-2,-3,-4,-5]; console.log(numbers1.some(isPositive)); // True console.log(numbers2.some(isPositive)); // False
-
array.find()
Just like
Every()
method,Find()
also checks if an element in the array pass a certain criteria. The only twist is, it returns the first element that passes the criteria. It takes in a function that mimics the criteria to be tested. If the function returns true for any of the element of the array, the method returns that value. If no array element meets the criteria, the method returnsundefined
.const isNegative = (number) => { return number < 0; } const numbers1 = [1,2,3,4,5]; const numbers2 = [1,2,-3,4,5]; console.log(numbers1.find(isNegative)); // undefined console.log(numbers2.find(isNegative)); // -3
-
array.includes()
It is one of the Javascript Array Functions that tells if an array contains a specific element or not. If the element is present it returns true, else false.
const numbers1 = [1,2,3,4,5]; console.log(numbers1.includes(2)); // true console.log(numbers2.find(9)); // false
-
array.reduce()
Reduce
method condenses the array into a single element. This always runs from left to right. As with all the methods till now, this too takes acallback function
as a parameter. Along withcallback function
( which from now onwards will be referred to ascbFunction
)reduce
method also takes an optional parameterinitialValue
. This is theinitialValue
the aggregation thatreduce
method will return.Suppose you need the sum of all elements in an array + 1, in that case, you will pass
initialValue
as 1.Let us discuss a bit about this
cbFunction
.
cbFunction
takes 4 parameters, 2 of which are optional.1. accumulator: It accumulates the return of
cbFunction
. It is the aggregation of values returned bycbFunction
in the previous invocation orinitialValue
if supplied.
2. currentValue: Current value for which thecbFunction
has been invoked.
3. index (optional): Index of the current element for which thecbFunction
is invoked.
4. array(optional): Array for which reduce has been invokedconst diff = (a,b) => { return a-b; } const numbers1 = [1,2,3]; console.log(numbers1.reduce(diff)); // -4 console.log(numbers1.reduce(sum,5)); // -1
-
array.reduceRight():
This method is exactly similar to
reduce
method, but it executes from right to left.const diff = (a,b) => { return a-b; } const numbers1 = [1,2,3]; console.log(numbers1.reduceRight(diff)); // 0 console.log(numbers1.reduceRight(sum,5)); // -1
-
Javascript has one of the largest communities among all the tools and programming languages. There are tons of features/methods which the language provides so that the developer can only focus on business logic. We have already covered some of the Javascript tricks in our articles here.
0 Comments