Minds
It is Recommended to practice while you learn

Map() Meathod

1) It applies a given function on all the elements of the array and returns the updated array. It is the simpler and shorter code instead of a loop.

2) array functions that transform the array according to the applied function and return the updated array.

Syntax :-

# array.map (function_to_be_applied)

# array.map (function (args)) {

//code

})



Example :-

function triple(n){
return n*3;
}
arr = new Array(1, 2, 3, 6, 5, 4);
var new_arr = arr.map(triple)
console.log(new_arr);


output :-

Output: [ 3, 6, 9, 18, 15, 12 ]

Reduce() Meathod

1) It reduces all the elements of the array to a single value by repeatedly applying a function. It is an alternative of using a loop and updating the result for every scanned element.

2) array functions that transform the array according to the applied function and return the updated array.

Syntax :-

# array.reduce (function_to_be_applied)

# array.reduce (function (args)) {

//code

})



Example :-

function product(a, b){
return a * b;
}
arr = new Array(1, 2, 3, 6, 5, 4);
var new_arr = arr.reduce(product)
console.log(product_of_arr);


output :-

Output: 720

Filter() Meathod

1) It filters the elements of the array that return false for the applied condition and returns the array which contains elements that satisfy the applied condition.

2) array functions that transform the array according to the applied function and return the updated array.

Syntax :-

# array.filter (function_to_be_applied)

# array.filter (function (args)) {

//code

})



Example :-

arr = new Array(1, 2, 3, 6, 5, 4);
var new_arr = arr.filter(function(x){
return x % 2 == 0
});
console.log(product_of_arr);
console.log(new_arr)


output :-

Output: [ 2, 6, 4 ]