Array Helper Methods in ES6
Array Helper Methods in ES6
Array Helper Methods can be used for complex tasks with array
very easily and can also easily work with complex arrays of
objects.
forEach()
map()
filter()
find()
some()
every()
reduce()
forEach(): The forEach() function is used to iterates through all
the entries of the array.
<script>
let sum = 0;
document.write(sum); // 15
</script>
Output:
15
<script>
Console.log(numbers);
</script>
Output:
2, 4, 6, 8, 10
const objects = [
{a:1, b:2},
{a:3, b:4},
{a:5, b:6}
];
return object.b;
});
document.write(onlybs); // [2, 4, 6]
</script>
Output:
2, 4, 6
<script>
let objects = [
{flag:1, a:1},
{flag:0, a:2},
{flag:1, a:3}
];
console.log("flag:" + object.flag
}
});
</script>
Output:
flag:1, a:1
flag:1, a:3
<script>
let objects = [
{flag:1, a:1},
{flag:0, a:2},
{flag:1, a:3}
];
objects.find((object) => {
if(object.flag === 0) {
console.log("flag:" + object.flag
});
</script>
Output:
flag:0, a:2
Note: The filter() method returns an array of objects and find() method
returns a single object.
some() :-
This array helper method checks if at least one of the elements satisfies the
provided condition. some() method returns a boolean value.
some() method executes the callback function on each and every element
until it finds one element for which the callback returns true.
This method returns true if one element from the array of elements satisfies
the condition. Otherwise it returns false.
The difference between find() and some() is, find() returns value
whereas some() returns true or false.
Syntax: arr.some((currentElement, index, arr) => {});
Example:
console.log(array.some(even));
// expected output: true
every() :-
This method checks if each and every element of the array satisfies the
condition and returns true only if all the elements of an array satisfies the
condition.
every() method executes a callback function on each and every element of
the array until it finds an element for which the callback returns false. If such
an element is found, it returns false. Otherwise it returns true.
In short, some() returns true if at least one element matches the criteria
whereas every() returns true only if all the elements match the criteria.
Syntax: arr.every((currentElement, index, arr) => {});
For the example used in some() method, every() returns false.
Example:
var result=array.every(element=>element%2==0);
console.log(result);
reduce() :-
This method executes a callback function that reduces an array into a single
element. The callback function to reducer() takes 4 arguments:
Accumulator: If the initial value is provided in the callback, the accumulator
value is set to the initial value. Otherwise, accumulator value will be set to
array’s first element’s value. This will hold the result of reduce function.
CurrentElement: It is the current element of the array.
Index: Current index of array.
Array: Source array.
Syntax: arr.reduce((currentElement, index, arr) => {}, initialValue);
Example:
var result=array.reduce((total,element)=>total+element);
console.log(result);
Example: (with initial value)
var arr = [1, 2, 4, 6, 8];
var result = arr.reduce((total, element) => total + element, 100);
console.log(result);
Output:
121