Map, Filter and Reduce in JavaScript

 //map() : Creates a new array by performing some operation on each array element.

let arr = [45, 23, 21];
// Array map method
let a = arr.map((value, index, array) => {
    console.log(value, index, array);
    return value + index;
});
console.log(a);

//filter() : Filters an array with values that passes a test. Creates a new array.
//Array filter method
let arr2 = [45, 23, 21, 0, 3, 5];
let a2 = arr2.filter((value) => {
    return value < 10;
});
console.log(a2);

//reduce() : Reduces an array to a single value
//Array reduce method
let arr3 = [1, 2, 3, 5, 2, 1];
let newarr3 = arr3.reduce((h1, h2) => {
    return h1 + h2;
});
console.log(newarr3);

OUTPUT:
45 0 [ 45, 23, 21 ] 23 1 [ 45, 23, 21 ] 21 2 [ 45, 23, 21 ] [ 45, 24, 23 ] [ 0, 3, 5 ] 14

Comments

Popular posts from this blog

Generators tutorial in Advance JavaScript

Document Object Module DOM querySelector and querySelectorAll tutorial in JavaScript

Find Even and Odd Numbers with Loops tutorial in JavaScript