Explain Map, Reduce and Filter functions.

Map, Reduce and Filter are three functions which operate on a given array of values.

  • Map function works like a projection, where we can project each value from the input set into some transformed output.
  • It takes a callback function which has two parameters - the current value and the index of the current value and expects a return value from the callback.
let arr = [1, 2, 3, 4, 5];
let result2 = arr.map(function (current, index) {
    return current % 2;
});
// output:
[ 1, 0, 1, 0, 1 ]
  • Reduce function works like an aggregate function, where we can create an aggregation from a given input set.
  • It takes a callback function which has three parameters - the previous value, the current value and the current index and expects a return value from the callback, which is applied onto the next array element iteration.
let result1 = arr.reduce(function (prev, current) {
    console.log(`${prev}, ${current}`);
    return prev + current;
});
// output:
1, 2
3, 3
6, 4
10, 5
result1: 15
  • Filter function is an extraction function where the input array is filtered based on a condition.
  • It takes a callback function which has two parameters - the current value and the current index and expects a boolean return value which represents whether the current element satisfies the condition or not.
let result3 = arr.filter(function (current, index) {
    return current % 2 === 0;
});
// output:
[ 2, 4 ]

Map, Reduce and Filter are three functions which operate on a given array of values.

  • Map function works like a projection, where we can project each value from the input set into some transformed output.
  • It takes a callback function which has two parameters – the current value and the index of the current value and expects a return value from the callback.
let arr = [1, 2, 3, 4, 5];
let result2 = arr.map(function (current, index) {
    return current % 2;
});
// output:
[ 1, 0, 1, 0, 1 ]
  • Reduce function works like an aggregate function, where we can create an aggregation from a given input set.
  • It takes a callback function which has three parameters – the previous value, the current value and the current index and expects a return value from the callback, which is applied onto the next array element iteration.
let result1 = arr.reduce(function (prev, current) {
    console.log(`${prev}, ${current}`);
    return prev + current;
});
// output:
1, 2
3, 3
6, 4
10, 5
result1: 15
  • Filter function is an extraction function where the input array is filtered based on a condition.
  • It takes a callback function which has two parameters – the current value and the current index and expects a boolean return value which represents whether the current element satisfies the condition or not.
let result3 = arr.filter(function (current, index) {
    return current % 2 === 0;
});
// output:
[ 2, 4 ]

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *