Thursday, September 15, 2022
HomeWordPress DevelopmentMap(),Filter(),scale back() with necessary interview questions

Map(),Filter(),scale back() with necessary interview questions


Map, scale back, and filter are all array strategies in JavaScript. Every one will iterate over an array and carry out a metamorphosis or computation. Every will return a brand new array primarily based on the results of the perform.
On this article you’ll find out about why and tips on how to use map, filter, scale back.

The map() methodology is used for creating a brand new array from an current one, making use of a perform to every one of many components of the primary array.



syntax:

var new_array = arr.map(perform callback(component, index, array) {
    // Return worth for new_array
}[, thisArg])
Enter fullscreen mode

Exit fullscreen mode

So, the unique arr.map perform takes a callback perform as an argument and that callback perform can have three arguments handed into it :
a. present worth
b. index of the present worth [optional]
c. array [optional]



Implementation of Map()

Callback perform will iterate for all of the values of array arr and modify the array and retailer the worth within the new array.
Utilizing map perform we want to not initialize new array.

Shall we say we now have an array and we wish one other array having all of the values double the earlier one.

const arr=[1,2,3,4,5];

perform double(x)
{
  return x*2;  
}
const output=arr.map(double);
console.log(arr);

Enter fullscreen mode

Exit fullscreen mode

writing the identical code utilizing arrow perform

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

Enter fullscreen mode

Exit fullscreen mode

The filter() methodology takes every component in an array and it applies a conditional assertion towards it. If this conditional returns true, the component will get pushed to the output array. If the situation returns false, the component doesn’t get pushed to the output array.
So, the unique Array.filter perform takes a callback perform as an argument and that callback perform can have three arguments handed into it :
a. present worth
b. index of the present worth [optional]
c. array [optional]

var new_array = arr.filter(perform callback(component, index, array) {
    // Return true or false
}[, thisArg])
Enter fullscreen mode

Exit fullscreen mode

Shall we say we need to filter out odd numbers from array.

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

perform isOdd(x)
{
   return x%2; 
}
const output=arr.filter(isOdd);

console.log(output);

Enter fullscreen mode

Exit fullscreen mode

One other instance we are able to take is, to search out numbers in array higher than 4.

const arr=[1,2,3,4,5]
const output=arr.filter((x)=>{ 
return x>4
});
console.log(output);

Enter fullscreen mode

Exit fullscreen mode

The callback argument is a perform that shall be known as as soon as for each merchandise within the array. This perform takes 4 arguments, however usually solely the primary two are used.

accumulator – the returned worth of the earlier iteration
currentValue – the present merchandise within the array
index – the index of the present merchandise [optional]
array – the unique array on which scale back was known as [optional]

The initialValue argument is non-compulsory. If supplied, it will likely be used because the preliminary accumulator worth within the first name to the callback perform.



Syntax

arr.scale back(callback[, initialValue])
Enter fullscreen mode

Exit fullscreen mode

Lets take instance for locating sum of all array components

const arr=[1,2,3,4,5];

const output=arr.scale back(perform(accumulator,currentIdx){
    acc+=curr;
    return acc;
});
console.log(output);

Enter fullscreen mode

Exit fullscreen mode

Lets clear our ideas by making an attempt some necessary query on these array strategies.

Q1. Given an array of objects customers, print fullname.

const customers=[
  {firstName:"john",lastName:"Biden",age:26},
  {firstName:"jimmy",lastName:"cob",age:75},
  {firstName:"sam",lastName:"lewis",age:50},
  {firstName:"Ronald",lastName:"Mathew",age:26},  
];
//Listing of fullname
const output=customers.map((x)=>{
    return x.firstName+" "+x.lastName;
})
console.log(output);
Enter fullscreen mode

Exit fullscreen mode

Q2. Given an array of objects customers, print person of explicit age together with their frequency.

const customers=[
  {firstName:"john",lastName:"Biden",age:26},
  {firstName:"jimmy",lastName:"cob",age:75},
  {firstName:"sam",lastName:"lewis",age:50},
  {firstName:"Ronald",lastName:"Mathew",age:26},  
];
Enter fullscreen mode

Exit fullscreen mode

Earlier than continuing ahead suppose 🤔 which array methodology we are able to use right here!
Analogy->Since we need to scale back our record to 1 object denoting all ages and discover completely different worth with rely of every worth therefore we are able to use ‘scale back’ right here.

const customers=[
  {firstName:"john",lastName:"Biden",age:26},
  {firstName:"jimmy",lastName:"cob",age:75},
  {firstName:"sam",lastName:"lewis",age:50},
  {firstName:"Ronald",lastName:"Mathew",age:26},  
];
//utilizing scale back array methodology
const output=customers.scale back(perform(acc,curr){
    if(acc[curr.age])
    //if current in array object
    {
        acc[curr.age]++;            
    }else{
    //if not current in array object
      acc[curr.age]=1;      
    }
    return acc;
},{})
console.log(output);
Enter fullscreen mode

Exit fullscreen mode

Q3.Print object having marks higher than 60 and rollNumber higher than 15.

let pupil =[
 {name:"Smith",rollNumber:31,marks:80},
 {name:"Jenny",rollNumber:15,marks:69},
 {name:"John",rollNumber:16,marks:35},
 {name:"Tiger",rollNumber:7,marks:55}
];
const particulars= pupil.filter((x)=>x.marks>60 && x.rollNumber>15);
console.log(particulars); 

Enter fullscreen mode

Exit fullscreen mode

This fall.Print sum of marks of all the coed

let pupil =[
 {name:"Smith",rollNumber:31,marks:80},
 {name:"Jenny",rollNumber:15,marks:69},
 {name:"John",rollNumber:16,marks:35},
 {name:"Tiger",rollNumber:7,marks:55}
];
const particulars = pupil.scale back((acc,curr)=>{
    return acc+=curr.marks;
},0);
console.log(particulars);
Enter fullscreen mode

Exit fullscreen mode



Chaining of array strategies is likely one of the speciality of map(),filter(),scale back().

Q5.Listing of all firstName from array whose age is greater than 30

const customers=[
  {firstName:"john",lastName:"Biden",age:26},
  {firstName:"jimmy",lastName:"Cob",age:75},
  {firstName:"Sam",lastName:"Lewis",age:50},
  {firstName:"Ronald",lastName:"Mathew",age:26},  
];
//Chaining
const output=customers.filter((x)=>x.age<30).map((x)=>x.firstName);
console.log(output);
Enter fullscreen mode

Exit fullscreen mode

Q6.Print the names of scholars who scored greater than 60

let pupil =[
 {name:"Smith",rollNumber:31,marks:80},
 {name:"Jenny",rollNumber:15,marks:69},
 {name:"John",rollNumber:16,marks:35},
 {name:"Tiger",rollNumber:7,marks:55}
];
const ans=pupil.filter((x)=> {
return x.marks>60}).map((x)=>{return x.title});
console.log(ans);

Enter fullscreen mode

Exit fullscreen mode

Q7.Print complete marks for college students with marks higher than 60 after 20 marks have been added to those that scored lower than 60

let pupil =[
 {name:"Smith",rollNumber:31,marks:80},
 {name:"Jenny",rollNumber:15,marks:69},
 {name:"John",rollNumber:16,marks:35},
 {name:"Tiger",rollNumber:7,marks:55}
];

const totalMarks=pupil.map((stu)=>{
    if(stu.marks<60){
        stu.marks+=20;
    }
    return stu;
}).filter((stu)=>stu.marks>60).scale back((acc,curr)=>acc+curr.marks,0);
console.log(totalMarks);
Enter fullscreen mode

Exit fullscreen mode

I hope that this weblog is perhaps useful to you. I’ve completed my finest to elucidate every array methodology with appropriate examples masking some essential inquiries to reply to ace your interview and achieve a deeper understanding.
Please go away any questions within the remark field, and I will do my finest to reply.
Should you discover this beneficial do like ❤️ it and observe me for extra such blogs.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments