Most Commonly Used Javascript Array Method's You Must Know

JS Array.methods()

Hey all, In these article we are going to take a look at JS Array methods which are most commonly used.

1) Array.map()

The map() method takes a callback function as a parameter and inside these callback function you can perform operation on the elements of an array and the result of these method is a newly created array i.e it doesn't modify the original array.The callback function accept's three parameters i.e element, index(optional) and array(optional,it's the array on which map operation is being performed). Let's take a look at syntax and an example below

Syntax

array.map(function callbackFn(element, index, array){ ... })

Example

const arr = [1,2,3,4,5]
const newArr = arr.map(ele=>ele+1);
console.log("arr",arr)  // output : [1,2,3,4,5]
console.log("newArr",newArr)  // output : [2,3,4,5,6]

2) Array.filter()

The filter() method takes a callback function as a parameter and returns a new array with the element which passes some condition provided in the callback function and if the condition is failed it return's a empty array.The callback function accept's three parameters i.e element, index(optional) and array(optional,it's the array on which map operation is being performed). Let's take a look at syntax and an example below

Syntax

array.filter(function callbackFn(element, index, array){ ... })

Example

const arr = [1,2,3,4,5]
const newArr = arr.map(ele=>ele>3);
console.log("arr",arr)  // output : [1,2,3,4,5]
console.log("newArr",newArr)  // output : [4,5]

3) Array.some()

The some() method is used to determine whether one of the element of array passes the condition which is provided in the callback function which is the parameter of the method.The method will return true if any one of the element of the array passes the condition otherwise it will return false.The callback function accept's three parameters i.e element, index(optional) and array(optional,it's the array on which map operation is being performed). Let's take a look at syntax and an example below

Syntax

array.some(function callbackFn(element, index, array){ ... })

Example

const arr = [1,2,3,4,5]
const isFivePresent = arr.some(ele=>ele===5);
console.log("isFivePresent",arr)  // output : true

const isSixPresent = arr.some(ele=>ele===6);
console.log("isSixPresent",arr)  // output : false

4) Array.find()

The find() method takes a callback function as a parameter and the callback function takes three parameters i.e element, index(optional) and array(optional,it's the array on which map operation is being performed). The find method returns the first array element which passes the condition provided in the callback function if none of the element passes the condition it returns undefined. Let's take a look at syntax and an example below

Syntax

array.find(function callbackFn(element, index, array){ ... })

Example

const arr = [{fruit:"apple",qty:5},{friut:"orange",qty:5},{friut:"mango",qty:6}]
let  fruitObj = arr.find(ele=>ele.qty===5);
console.log("fruit:",fruit) // output fruitObj:{fruit:"apple",qty:5}}

const arr1 = [{fruit:"app",qty:5},{friut:"orange",qty:5},{friut:"mango",qty:6}]
fruitObj = arr1.find(ele=>ele.qty===5);
console.log("fruit:",fruit) // output fruitObj:{fruit:"orange",qty:5}}

5) Array.indexOf()

The indexOf() method two takes parameter i.e searchElement & fromIndex(optional), these method returns the first index of the element at which it can be found in the array and if the element is not found it returns -1. Let's take a look at syntax and an example below

Syntax

array.indexOf(searchElement, fromIndex)

Example

const arr = [1,3,5,1,9,8]
console.log(arr.indexOf(1))  // output 0
console.log(arr.indexOf(1,2)) // output 3
console.log(arr.indexOf(20)) // output -1

6) Array.includes()

The includes() method two takes parameter i.e searchElement & fromIndex(optional), these method returns true if the element is found in the array and returns false if the element is not found. Let's take a look at syntax and an example below

Syntax

array.includes(searchElement, fromIndex)

Example

const arr = [1,3,5,1,9,8]
console.log(arr.includes(1))  // output true
console.log(arr.includes(20)) // output false

Above are some of the most common used javascript array methods. I hope you find these article helpful.Goodluck!