Quickupdate

  • Home
  • Top Trending
    • Top Android Apps
    • Top Ios Apps
  • Featured
  • About Us
  • Contact us
  • Privacy Policy and Disclaimer

Friday, 6 August 2021

Map Filter & Reduce in Javascript

 Developers     August 06, 2021     Javascript     No comments   

  1. Map, Filter, Reduce are the higher-order function in javascript. 

Map

  1. The map function is used to transform an array.
  2. Suppose you have this array

const arr = [5, 1, 3, 2, 6]

    3. Its possible transformations could be doubling, tripling, or finding a Binary number of each element present in the array. and it will store the transformation into the new array.

    4. 
function double(x) {
    return x * 2;
}

/*
* function map accepts function `double`.
* so the map is Higher Order function
*/
const output = arr.map(double); 
console.log(output);


    5. You can also write the above function like this

const output = arr.map((x) => x * 2);
console.log(output);


Map Example


const users = [
    { firstName: "Rahul", lastName: "More", age: 22 },
    { firstName: "Yogesh", lastName: "More", age: 20 },
    { firstName: "Rohit", lastName: "Bhosale", age: 17 },
    { firstName: "Nityanand", lastName: "Yevte", age: 28 },
    { firstName: "Sachin", lastName: "Yevte", age: 26 },
];

// get Full name of users
const fullName = users.map((x) => { // Now x has access to every elemet of array.
    console.log(x.firstName + " " + x.lastName)
});


Filter()

  1. The filter function is used to filter values present inside the array.
  2. Let's say if you want to filter out al the values which are odd, or greater than 3 inside it, or divisible by 3.
  3. Used to filter the data based on condition.
function isOdd(x) {
    return x % 2;
}
const FilterData = arr.filter(isOdd)
console.log(FilterData);


function even(x) {
    return x % 2 == 0
}

const FilterData = arr.filter(even)

function even(x) {
    return x % 2 == 0
}

const FilterData = arr.filter(even)

function greaterThan(x) {
    return x > 4;
}
const FilterData = arr.filter(greaterThan)



Filter Example


// Get the name of the users Whose age is greater than 20

const ageOfx = users.filter((x) => {
    if (x.age > 20) {
        console.log(x.firstName);
    }
})

Reduce()


  1. As the name suggests Reduce doesn't Reduce anything :D
  2. Reduce can be used to iterate over each & every element of an array & find the sum of all the elements in the array. or the largest or the max element inside the array.
  3. Instead of directly going to write Reduce Method we will just check the above (p2) example in a Non-Functional way.

function findSum(arr) {
    let sum = 0;
    for (var i = 0; i < arr.length; i++) {
        sum = sum + arr[i];
    }
    return sum
}
console.log(findSum(arr));

    4. So to find we do something like this in a non-functional way.

    Let's Do it in a Functional Way.
    

const res = arr.reduce(function (acc, curr) { // Key, Value

});


    5. So first function `reduce` accepts 2 parameters 1st is a function & we will talk later about 2nd function.

    6. initially `acc` in 0 & `curr` contains the elements of the array.
    7. if we console acc initially it will give 0 & later it will return `undefined.

    8. 

const res = arr.reduce(function (acc, curr) { // Key, Value
    acc = acc + curr;
    return acc;
});
console.log(res);

    9. & if we talk about 2nd argument it takes the initial value of acc. ( by default it is 0 )

    10. Another example using reduce. Find max element using Reduce. 

const maxUsingReduce = arr.reduce(function (max, curr) {
    if (curr > max) {
        max = curr;
    }
    return max;
});
console.log(maxUsingReduce);


If my curr element is greater than the max element. then my max element is the current element.


Reduce Example

// get The age & show the count of number of people present in that array! 

const countAge = users.reduce(function (acc, curr) {// intialValue, currentValue
    if (acc[curr.age]) {
        acc[curr.age] = ++acc[curr.age];
    } else {
        acc[curr.age] = 1
    }
    return acc;
}, {});
console.log(countAge)


   We can also use Chaining on these functions.


// Get the name of the users Whose age is greater than 20

const ageOfx = users.filter((x) => x.age > 20).map(x => x.firstName);
console.log(ageOfx);




Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

Popular Posts

  • How to upload the existing folder on GitHub Or GitLab?
    These are the steps to upload the existing folder on GitHub Or GitLab. Whenever you want to push your existing folder to git or GitHub you m...

Categories

  • FAANG (2)
  • Javascript (6)
  • Node (1)
  • Project Management (1)
  • React (9)
  • SQL (1)
  • Testing (1)

Blog Archive

  • January 2023 (1)
  • January 2022 (1)
  • November 2021 (3)
  • October 2021 (3)
  • August 2021 (1)
  • July 2021 (7)
  • June 2021 (12)
  • February 2021 (1)
  • January 2021 (1)
  • January 2020 (3)
  • August 2019 (3)