Javascript Array Methods - 1

Javascript Array Methods - 1

Umut Cakmak
·Oct 24, 2021·

Subscribe to my newsletter and never miss my upcoming articles

some()

With the some() method, we can loop the array elements and subject each array element in turn to a condition we specify. If even one of the array elements satisfies the condition, true is returned. If there is no array element that satisfies the condition, false is returned.

Definition:

array.some(callback(element[, index[, array]])[, thisArg])

value : This parameter is required and holds the value of the array elements.

index : This parameter is optional and is the index number of the array elements

array : This parameter is optional and holds the array itself.

const a = [1, 2, 3, 5, 10].some(item => item > 5)
const b = [1, 2, 3, 4, 5].some(item => item > 5)

console.log(a)
console.log(b)

Output
---------
true
false

every ()

The "every" method is somewhat similar to the "some()" method. If all array elements satisfy the condition, true is returned. If there is even one array element that does not satisfy the condition, false is returned.

Definition:

array.some(callback(element[, index[, array]])[, thisArg])

value : This parameter is required and holds the value of the array elements.

index : This parameter is optional and is the index number of the array elements

array : This parameter is optional and holds the array itself.

const a = [1, 2, 3, 5, 10].every((item) => item > 0);
const b = [1, 2, 3, 4, 5].every((item) => item > 5);

console.log(a);
console.log(b);

Output
---------
true
false

map()

For each element of the array; It calls the function (callback) given as a parameter and creates a new array according to the points specified in the function.

Definition:

arr.map(callback( currentValue[, index[, array]])[, thisArg])

  • The map() method creates a new array for each array element with the results of calling a function.
  • The map() method sequentially calls the provided function once for each item in an array.
  • map() does not execute the function for empty items.
  • map() does not modify the original array.
const numbers = [1, 2, 3, 4, 5]
const doubled = numbers.map(value => value * 3)

console.log(doubled)

Output
---------
(5) [3, 6, 9, 12, 15]

filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Definition:

arr.filter(callback(element[, index, [array]])[, thisArg])

  • A new array with the elements that pass the test. If no elements pass the test, an empty array will be returned.
  • filter() does not mutate the array on which it is called.

value : This parameter is required and holds the value of the array elements.

index : This parameter is optional and is the index number of the array elements

array : This parameter is optional and holds the array itself.

let results = arr.filter(function(element, index, array) {
   // returns true if the elements fit the filter.
});

forEach()

We can use the forEach method to loop the array elements and process them in order.

Definition:

arr.forEach(callback(currentValue [, index [, array]])[, thisArg])

let numbers = ["32","10","71","98","25"];

 numbers.forEach(function (value, index, array) {

 console.log("Value : " + value + " " + "Index : " +index + " " + "Array : " + array);

});

Output
---------
Value : 32 Index : 0 Array : 32,10,71,98,25 
Value : 10 Index : 1 Array : 32,10,71,98,25 
Value : 71 Index : 2 Array : 32,10,71,98,25 
Value : 98 Index : 3 Array : 32,10,71,98,25 
Value : 25 Index : 4 Array : 32,10,71,98,25

Thanks for reading :)

Resources

mdn

Javascript Info