Most useful array method: filter and map

Array: filter and map method
Array Filter and Map method for operation on array

Map and filter method used for operation on the array. Like select element or operation each element, Before filter and map method we use normally for and forEach method.

In Javascript array is one type of database that store list of element inside. Array support multiple prototype method like join(), filter(), map(), push(), pop(), fill(), include(), splice() and many more.

Today we are going to discuss two most use full method one is filter() and other is map().

map()

Most popular and all developer use this method for change array element. Let take one small example.

var jsDeveloper = ["Developer", "Full stack developer", "for developer", "top developer"];

jsDeveloper = jsDeveloper.map(function(element) {
	return 'Javascript Everywhere ' + element;
});

console.log(jsDeveloper);

[
"Javascript Everywhere Developer",
"Javascript Everywhere Full stack developer",
"Javascript Everywhere for developer",
"Javascript Everywhere top developer"
]

Don’t worry, Syntax is very simple.

var testArray = arr.map(function);

Developer asks me a question related to map(), Which question do you know? Question is: Can you use a map() method with string? And our answer is yes we can. let understand with one example.

const jsgrip = "Javascript Developer"

const newName = Array.prototype.map.call(jsgrip, eachLetter => {
    return eachLetter + 1
})

console.log(newName)

This is default prototype Array.prototype is accessible to multiple method and we use map method here.

Filter()

Filter also method of array. Filter is like you can say search as well.

var numberArray = [1, 2, 7, 10, 15, 25, 60, 100, 1];

var filterNumber = numberArray.filter(function(number) {
  return number > 10;
});
console.log(filterNumber)

[15, 25, 60, 100]

In Filter() we should pass one argument argument consider as current element of array and base on this current element we put condition and return whatever result for array.

But developer are confuse on map and filter function / method. Developer question is what is different between map and filter?

Map() takes all objects in a list and allows you to apply a function on it. Filter() takes all objects in a list and runs that through a function to create a new list with all objects that return True in that function, Filter never creates a new array with return false.

As a developer, you should ask a question and clear you all confusion we are always helping the developer and make things easier.

Before map() and filter() method we are use for loop for search and map. Let’s take one example.

var numberArray = [1, 2, 7, 10, 15, 25, 60, 100, 1];
var tempArray = [];
for (var i = 0; i < numberArray.length; i++){
  // for Filter
  if (numberArray[i] > 7) {
    tempArray.push(numberArray[i]);
  }

  // for map
  // tempArray.push(numberArray[i] * 2);
}

console.log(tempArray);

Just give me comment if any confusion on map() and filter()