Array Methods

Array Methods

How do array Methods make life easier?

What is Array?

An Array is an object that can store multiple values at once.JavaScript arrays are resizable and they can contain a mix of different data types. the array is a primitive data type. the element of the array accessible by using an index. Index starts from 0

The array has so many methods. let's discuss them one by one

  • concat() :

    This method returns a new array that is the calling array joined with other arrays or values. This method is used to join two different arrays from end to end.

let firstArray=[raam, sham, shashi]
let secondArray=[sunil,hari]
let thirdArray=firstArray.concat(secondArray)
console.log(firstArray)   //[raam,shaam,shashi]
console.log(secondArray) // [sunil,hari]
console.log(thirdArray) //[raam,shaam,shashi,sunil,hari]

in the above code snippet, you can see thirdArray is returning a new array we are contacting.

  • find() :

The find() method returns the first element in an array that satisfies a provided function.it returns undefined when none of the elements satisfies the function. it takes three parameters - callback function to execute each element of the array - element the current element of the array - thisarg(optional) object to use this inside callback

let array=[1,2,4,8,3]
const findIseven=array.find(element=>element%2===0)
console.log(findIseven) // 2 

// undifined output

let array=[1,2,4,8,3]
const findIseven=array.find(element=>element%5===0)
console.log(findIseven) // undifined
  • some():

Some() method returns true if at least one element in the calling array satisfies the provided function otherwise it returns false . it does not change the original array.

1. let array=[2,3,4,8]
const isOdd=array.some(value=>value%2!==0)
console.log(isOdd) // true

2. let fruits=["apple","banana","mango","grasp"]
const isFruit=fruits.some(fruit=>fruit==="painaple")
console.log(isFruit) // false
  • filter():

The filter () method returns a new array that filters the elements that pass the test defined by the given function. filter() does not change the original array. it takes the 3 parameters

  • callback the test function to execute on each array element returns true if the element passes the test else it returns false.

  • element the current element being passed from the array.

  • thisarg(optional)

let array=[2,3,7,22,1,3,6,8,10]
let isEven=array.filter(element=>element%2===0)
console.log(isEven) // [2,22,6,8,10]

we can see it filters all the even numbers from the array in the above code snippet.

  • map():

map() method iterates over the array and returns a new array with the result of calling a function for it does not change the original array.

let array=[2,3,4,5,6,7]
const sqrElement=array.map((element)=>element*element)
console.log(sqrElement) //  [4, 9, 16, 25, 36, 49]

here we can see the map () returning a new array of all element squares.

**Above we have seen some methods of array apart from this the array has so many methods here below I have listed **

List of other array methods:

  • sort(): this method helps to sort the element it also returns a sorted array.

  • pop (): pop method help for removing the last element of the array.

  • push(): push method helps add one or more elements to the array at the end of an array, and return a new length of the array.

  • reverse(): it reverses the element of the array first element becomes the last, and the last element becomes the first.

  • shift (): shift method remove the first element from an array and returns that element.

  • unshift(): this method adds one more element to the array in the first index. and returns a new length of the array.

  • reduce(): The reduce method executes the reducer function on each element of an array and returns a single output value.

conclusion :

This is all about array and array different methods hope this blog will help you to understand the array methods. not only do these method arrays have many methods go through the mdn Docs to explore more about the array methods.