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 ]
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.

Privacy Overview
Referbruv

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

3rd Party Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.

Keeping this cookie enabled helps us to improve our website.