+ 2

What is the function of map

Map

9th Apr 2018, 5:08 AM
Diwash Subedi
Diwash Subedi - avatar
3 Answers
+ 5
In many programming languages, map is the name of a higher-order function that applies a given function to each element of a list, returning a list of results in the same order. It is often called apply-to-all when considered in functional form.
9th Apr 2018, 6:10 AM
᠌᠌Code X
᠌᠌Code X - avatar
+ 3
In normal arrays, you use an int to access your elements, right? In a map, that mustn't be an int.
9th Apr 2018, 5:39 AM
Timon Paßlick
+ 2
Hi Diwash Map can be used to iterate through an array or object without using loops. As Brains says this is very common in Functional Programming. Here is an JavaScript example: // an array of objects called animals // var animals = [{ name: "Bailey", size: "small" }, { name: "Max", size: "medium" }, { name: "Charlie", size: "large" }, { name: "Buddy", size: "small" }, ]; //Lets say we want to print out "name of dog is size" //first we create a function to operate on each element of the array function findSize(x){ return x.name+" is "+x.size; } //then we can map each element of the animals array with our function var [...names] =animals.map( findSize); console.log(...names); //"Bailey is small Max is medium Charlie is large Buddy is small " In ES6 we can use arrow functions to make this look nice and compact var [...names] =animals.map( x => x.name+" is "+x.size); console.log(...names);
9th Apr 2018, 10:23 AM
Mike Choy
Mike Choy - avatar