ARRAY HELPER METHODS

Array Helper Methods :

These Array helper methods were introduced in ES6 which is used for data manipulation.

forEach( ) :

The forEach( ) method accepts a function that iterates through the array and calls this function for every element of the array.

Example :

const arr = [3, 5, 6, 1];
let sum = 0;
arr.forEach((item) => {
sum += item;
});
console.log(sum)

map( ) :

This method returns a new array with the results of a function that is passed to it. map( ) calls the function for every element of the array in particular order.

Example :

const arr = [3, 5, 6, 1];
const multiply = arr.map((item) => {
return item * 2
});
console.log(multiply);
console.log(arr);

filter( ) :

This method takes a function along with it and this function contains a boolean condition. filter( ) returns an array consisting of all the elements that fall in the condition stated. filter() does not make any changes to the original array.

Example :

const arr = [3, 5, 6, 1];
const req = arr.filter((item) => {
return item > 4
});
console.log(req);
console.log(arr);

reduce( ) :

The reduce( ) function calls a function that we provide as a reducer, on each element of the array. It results in a single output value based on the reducer function operation.The reduce method takes two parameter i.e the first parameter is the iterator function, the second parameter is the initial value. The iterator function in a reduce() takes two parameters i.e accumulator and current. Accumulator is total of all calculations and current is current iteration/value. Always return accumulator.

Example :

const arr = [3, 5, 6, 1];
const req = arr.reduce((acc, curr) => {
acc += curr
;
return acc
;
},0);
console.log(req);

slice() :

The slice( ) method returns the selected elements in an array, as a new array object. It selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

Syntax

array.slice(start, end);

Example :

let fruits=["Banana", "Orange", "Lemon", "Apple","Mango"];
let res=fruits.slice(1,3);
console.log(res)
//Prints:["Orange", "Lemon"]
console.log(fruits.slice(0,4))
//Prints:["Banana", "Orange", "Lemon", "Apple"]
//-1 returns the last element
console.log(fruits.slice(-1))
//Prints: ["Mango"]

splice() :

The splice() method adds/removes items to/from an array, and returns the removed item(s). This method changes the original array.

Syntax

array.splice(index, howmany, item1, …., itemX)

index - Required. An integer that specifies at what position to add/ remove items, Use negative values to specify the position from the end of the array.

howmany - Optional. The number of items to be removed. If set to 0, no items will be removed

item1, …, itemX - Optional. The new item(s) to be added to the array.

Example :

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0, "Lemon", "Kiwi");
console.log(fruits);
//Prints: ["Banana", "Orange", "Lemon", "Kiwi", "Apple", "Mango"]
//At position 2, add the new items, and remove 1 item:
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2, 1, "Lemon", "Kiwi");
console.log(fruits);
//Prints: ["Banana", "Orange", "Lemon", "Kiwi", "Mango"]
//At position 2, remove 2 items:
var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
fruits.splice(2, 2);
console.log(fruits);
//Prints: ["Banana", "Orange", "Kiwi"]

find() :

The find() method returns the value of the first element in an array that pass a test.

The find() method executes the function once for each element present in the array:

  • If it finds an array element where the function returns a true value, find() returns the value of that array element and does not check the remaining values.
  • Otherwise it returns undefined

Note : find() does not execute the function for empty arrays and find() does not change the original array.

Example :

let array = [10, 20, 30, 40];
let found = array.find((elem) => {
return elem > 20;
});
console.log(found);
//Prints: 30
let undef = array.find((elem) => {
return elem > 40;
});
console.log(undef);
//Prints: undefined

indexOf() :

The indexOf() method searches the array for the specified item, and returns its position. The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array.

Returns -1 if the item is not found. If the item is present more than once, it returns the position of the first occurrence.

Syntax

array.indexOf(item, start)

item - the item to search for

start - Optional. Where to start the search. Negative values will start at the given position counting from the end, and search to the end.

Example :

let array = [10, 20, 30,20];
console.log(array.indexOf(20))
//Prints: 1
console.log(array.indexOf(20, 2))
//Prints: 3
console.log(array.indexOf(40))
//Prints: -1