5 must known array method

Javascript array map() function

The map() function runs a given function once for each element in an iterable. It does not modify the array it is invoked on.

The syntax for the map() method is :

map(function callback(currentValue, index, array)) { … })

Arrow function:

array.map(( currentValue, index) => { … })

Parameters:

Callback - The function that is invoked for each element of the array. The arguments passed in the callback function are: -

  • currentValue - Gives the array's current element being processed.
  • index (Optional) - Gives the index of the array's current element being processed.
  • array (Optional) - The array map was called upon.

Returns a new array with elements calculated from the callback function for each element.

Example:

const num= [1, 2, 3, 4, 5]
const squared= num.map(value => value * 2)
console.log(squared)  // Output: [1, 4, 9, 16, 25]

Using arrow function -

[1,2,3,4,5,6].map(item => item * 3)

Output - [3, 6, 9, 12, 15, 18]

Javascript array filter() function

It generates a new array with all the elements that pass the callback function's test. The callback function is a condition that is used to test each array element. Array elements that fail the callback test are simply skipped and not included in the new array.

The syntax for the filter() method is :

filter(function callback(currentValue, index, array)) { condition})

Using Arrow function:

array.filter(( currentValue, index) => { condition })

Parameters:

  • currentValue - The array's current element is being processed.
  • index (Optional) - The index of the array element currently being processed.
  • array (Optional ) - The array filter was called upon.

The elements that pass the test are added to a new array. An empty array will be returned if no elements pass the condition.

function votingAge(value) {
  return value >= 18
}
let filtered = [12, 5, 8, 130, 44].filter(votingAge)

Note - filter() does not change the original array.

Javascript array reduce() function

The reduce() method applies a reducer function (that you specify) to each element of the array and returns a single output value. The reducer function takes four arguments:

  • accumulator
  • current Value
  • current Index
  • source Array

The accumulator is allocated the value returned by your reducer function, which is remembered over each iteration of the array and finally becomes the final, single result value. The syntax of the reduce() method is:

array.reduce(callback(accumulator, currentValue), initialValue)

Example :

[1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array) {
  return accumulator * currentValue
})

Output - 24 // 123*4

Javascript array some() function

The some() method determines whether at least one element in the array passes the given function's test. If it finds an element in the array for which the given function returns true, it returns true; otherwise, it returns false. It makes no changes to the array.

The syntax for the some() method :

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

Using arrow function -

array.some((element, index) => { ... } )

The callback is invoked with three arguments:

  • the value of the element,
  • the index of the element,
  • the Array object that is being traversed.

Example:

function votingAge(element, index, array) {
  return element > 18;
}

[9,5,1,7,2].some(votingAge);  // false
[19,23,4,1,3].some(votingAge); // true

Javascript array every() function

Every method verifies that every element in an array passes a series of tests. If all of the elements pass the set, this method will return true. The Syntax for every() method :

every(function callback(element, index, array){ ... })

Using arrow function -

array.every((element, index) => { ... } )

Note- every() does not mutate the array on which it is called.