DEV Community

PUSHAN VERMA
PUSHAN VERMA

Posted on

Higher Order Functions

Learning about MAP,FILTER
Map

// higher order functions

//Map Functions->

//๐Ÿ“ŒMap is itself a function
// Map takes a callback function as argument
//Map will call the callback function as many times as the elements in the array
//map will process every value and will apply the instructions that inside the callback function
//map returns a new array and doesnt make changes in original array
//it does not chagnge the original array

//๐Ÿ“ŒQ-1
let arr =[2,3,4,5,6,7,8,9];

let squarr =arr.map(function square(v){
return v*v;
})

//๐Ÿ‘‰ans -->[
// 4, 9, 16, 25,
// 36, 49, 64, 81
// ]

console.log(squarr);
console.log(arr);

//๐Ÿ“Œ Q-2

// You have to use map function and you will take out firstName and lastName separately

let name1=['Pushan Verma','Chetna Singh','Aditi Srivastava' ,'Chandresh Tomar'];

let modifiedarr=name1.map(function(n)
{
let splitting=n.split(" ");
return splitting
})

console.log(modifiedarr);

// ๐Ÿ‘‰ans -->[
// [ 'Pushan', 'Verma' ],
// [ 'Chetna', 'Singh' ],
// [ 'Aditi', 'Srivastava' ],
// [ 'Chandresh', 'Tomar' ]
// ]

// let modifierarr1 =name1.map(function(n)
// {
// let splitting1=n.split(" ");
// let first =
// })

//Q-3
// you have transaction array and you have to implement map function to convert all this ruppees into dollars
// dollar to rs
const transactions = [1000, 3000, 4000, 2000, -898, 3800, -4500];
const inrtToUsd = 74;

const modifiedarr2 =transactions.map(function(n)
{
const value =(n/inrtToUsd).toFixed(2);
return value;
})

console.log(modifiedarr2);

// ๐Ÿ‘‰ans -->

//๐Ÿ‘when tofixed is 0
// [
// '14', '41',
// '54', '27',
// '-12', '51',
// '-61'
// ]
//๐Ÿ‘when tofixed is 2
// [
// '13.51', '40.54',
// '54.05', '27.03',
// '-12.14', '51.35',
// '-60.81'
// ]

FILTER

//๐Ÿ“ŒFilter
//filter returns a new array containing array elements that matches a specified condition

//it doesnot change the original array
//for loop ki condition ki jesi hai yeh
//jo value true hongi wahi array mei jakar gir jayengi , aur jinki condition satisfy nahi hongi wo discard ho jauengi

// ๐Ÿ“ŒQ-1
let arr=[2,3,4,5,6,7,8,9];

let ans =arr.filter(function(n)
{
if(n%2==0)
{
return true;
}
else
{
return false;
}
}
)

console.log(ans);

// ๐Ÿ‘‰ans ->[ 2, 4, 6, 8 ]

//๐Ÿ“ŒQ-2

//Filter out the profit in transactions i.e the +ve ones

const transactions = [1000, 3000, 4000, 2000, -898, 3800, -4500];

let profit=transactions.filter(function(n){

if(n>0)
{
    return true;
}
else
{
    return false;
}
Enter fullscreen mode Exit fullscreen mode

})

console.log(profit);

//๐Ÿ‘‰ ans --> [ 1000, 3000, 4000, 2000, 3800 ]

FILTERMAP CHAIN

//๐Ÿ‘‰ Q-those who are female , we have to return their age
let arr = [
{name: "A", age: 14, gender: "M"},
{name: "B", age: 34, gender: "M"},
{name: "C", age: 24, gender: "F"},
{name: "D", age: 44, gender: "F"},
{name: "E", age: 44, gender: "M"},
{name: "I", age: 28, gender: "F"},
{name: "G", age: 36, gender: "M"},
{name: "H", age: 47, gender: "F"}
]

let ladiesage =arr.filter(function(n)
{
if(n.gender=='F')
{
return true;
}
else
{
return false;
}
})

console.log(ladiesage);

// ๐Ÿ‘‰ans -->[
// { name: 'C', age: 24, gender: 'F' },
// { name: 'D', age: 44, gender: 'F' },
// { name: 'I', age: 28, gender: 'F' },
// { name: 'H', age: 47, gender: 'F' }
// ]

let finalans =ladiesage.map(function(n){
return n.age;
})

console.log(finalans);

// ๐Ÿ‘‰ans -->[ 24, 44, 28, 47 ]

//this was the naive approach to the problem

//now we will study this

//๐Ÿ“Œ๐Ÿ“ŒFILTER MAP CHAIN

let ladiesage1 =arr.filter(function(n)
{
if(n.gender=='F')
{
return true;
}
else
{
return false;
}
}).map(function(n){
return n.age
})

console.log(ladiesage1);

// ๐Ÿ‘‰ans --> [ 24, 44, 28, 47 ]

Small Functions like Some,FIND AND EVERY

Some

//๐Ÿ“ŒSome function

//some elements should follow the condition . ie if any of the one follow the condition then it will return true

const transactions = [1000, 3000, 4000, 2000, -898, 3800, -4500];

//displaying element that is +ve
let ans =transactions.some(function(n)
{
return n>0
})

console.log(ans);

// ๐Ÿ‘‰ans -->true

const transactions1 = [-1000, -3000, -4000, -2000, -898, -3800, -4500];

//displaying element that is +ve
let ans1 =transactions1.some(function(n)
{
return n>0
})

console.log(ans1);

//๐Ÿ‘‰ ans ->false

EVERY

//๐Ÿ“Œevery function

//every element should follow the condition

//if all the elements follow the condition then only it will return true
const transactions = [1000, 3000, -4000, 2000, -898, 3800, 4500];

//displaying element that is +ve
let ans =transactions.every(function(n)
{
return n>0
})

console.log(ans);

//๐Ÿ‘‰ ans -->false

const transactions1 = [-1000, -3000, -4000, -2000, -898, -3800, -4500];

//displaying element that is +ve
let ans1 =transactions1.every(function(n)
{
return n<0
})

console.log(ans1);

// ๐Ÿ‘‰ans ->true

FIND
//๐Ÿ“Œfind

// it just works same as filter , but it doesnot give array , it finds the first value according to the condition and return

const transactions = [1000, 3000, 4000, 2000, -898, 3800, -4500];

let ans =transactions.find(function(n)
{
return n<0;
})

console.log(ans);

// ๐Ÿ‘‰ans -->-898

Top comments (0)