
MAP:
The map()
method is used for creating a new array from an existing one, applying a function to each one of the elements of the first array.
const numbers=[1,2,3,4,5]
const results=numbers.map((x)=>{
return x;
});
console.log(results) ;//output [1,2,3,4,5]
FILTER:
The filter()
method takes each element in an array and it applies a conditional statement against it. If this conditional returns true, the element gets pushed to the output array. If the condition returns false, the element does not get pushed to the output array.
const arr=[22,11,3,4,6]
const evens=arr.filter((x)=>{
return x%2===0;
});
console.log(evens);//output even number[22,4,6]
REDUCE:
The reduce()
method reduces an array of values down to just one value. To get the output value, it runs a reducer function on each element of the array.
Example;
const numbers=[1,2,3,4] const sum=numbers.reduce((result,item)=>{ return result+item; },0); console.log(sum);//output(10)