Js Array Common Method 2
1. forEach
The forEach()
method executes a provided function once for each array element.
2. map
The map()
method creates a new array populated with the results of calling a provided function on every element in the calling array.
The map()
method creates a new array
3. every and some
The every()
method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.
The some()
method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
4. reduce /reduceRight
The reduce()
method executes a user-supplied “reducer” callback function on each element of the array, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
Perhaps the easiest-to-understand case for reduce()
is to return the sum of all the elements in an array.
The reducer walks through the array element-by-element, at each step adding the current array value to the result from the previous step (this result is the running sum of all the previous steps) — until there are no more elements to add.
5. indexOf / lastIndexOf
The indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present.
Last updated