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 :
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 :
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 :
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 :
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 :
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 :
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 :
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 :