How does the map function work?

Asked

Viewed 5,438 times

3

I’m studying Javascript, learning the functions map(), filter() and reduce(), and found a article in medium, the problem is that I can not understand why this part of the code return undefined. I’ve even tried to put the full code on pythontutor and still nothing to understand.

const students = [
    { name: 'Anna', grade: 6 },
    { name: 'John', grade: 4 },
    { name: 'Maria', grade: 9 }
];

var byName = function(object) {
    return object.name
};
console.log(byName(students));

I expected this :

// Aqui retornaria a quantidade de name's dentro de students.
var byName = function(object) {
  return object.name; 
};

// Aqui retornaria o valor dentro de cada name ("Anna", "John" e "Maria")
var byNames = function(list) {
  return list.map(byName);
};
  • What did you expect console.log(byName(students)); return? (just to see how you’re thinking)

  • When you say "// Here would return the amount of name’s within Students." You mean you wait for the answer 3 or an array with the 3 names?

  • Sorry @Sergio, I didn’t notice that you had responded. Sorry !! I expected an array with the amount of objects inside Students. So I could do something like: Students[0]. name and return to me "Anna"

  • I’m still not sure you have the answer you were looking for... but anyway so if you want to do it students[0].name and get Anna you don’t need functions... you can do it directly on the object. Or I got it wrong?

  • Actually I asked the question in a stupid way kkk. I wanted to understand how byname(Students); returns Undefined and byNames(Students); returns the 3 names inside the object, when the return of byNames is list.map(byname); and byname(Students) returns Undefined.

  • byName(students); returns Undefined because this function looks for the property in the array you passed to it. is the same as students.objeto; that gives Undefined. If you use byNames(students); then it will work as you want with the principles of functional programming.

  • I’m beginning to understand. I’m going to study more!!

Show 2 more comments

1 answer

5


Map

The function map maps the elements of an array to a new array with the result of a function applied to each element.

Taking the example of the documentation, we can consider an array of numbers and use the map to double each of them as follows:

var numeros = [1, 5, 10, 15];
var dobros = numeros.map(function(x) {
   return x * 2;
}); 

console.log(dobros); //2, 10, 20, 30

We see that the function is called for each element of the array numeros, thus building a new element for the array dobros. This element is always the parameter of the function used, which in the above example is called x, although it may have any other name.

Problem in your code

The problem is that the function byName expects to receive an object that has the property name and that in your example has not, because you passed students which is the whole array:

console.log(byName(students));

To pass a specific student could do so:

console.log(byName(students[0]));

Who would be getting the name of the first student.

Example:

const students = [
    { name: 'Anna', grade: 6 },
    { name: 'John', grade: 4 },
    { name: 'Maria', grade: 9 }
];

var byName = function(object) {
    return object.name
};
console.log(byName(students[0]));

However the objective of this exercise was not even this, as it is mentioned in the article you mentioned, but rather to map the list of students to a list of names through the function map and byName with:

students.map(byName);

That maps from the original array of students, which is an array of objects, to a new one with only the names, using the function byName returns only the name of each element.

Example:

const students = [
    { name: 'Anna', grade: 6 },
    { name: 'John', grade: 4 },
    { name: 'Maria', grade: 9 }
];

var byName = function(object) {
    return object.name
};

console.log(students.map(byName));

Could even create a second function for this mapping of the whole list:

var byNames = function(list) {
    return list.map(byName);
};

And then use it properly:

byNames(students); // ["Anna", "John", "Maria"]

Map with Arrow functions

Another even more simplified way using Arrow Functions of ES6 would be:

students.map(x => x.name);

Which would make it not even necessary to have the function byName

Applying Arrow functions to the initial example of the folds would look like this:

var numeros = [1, 5, 10, 15];
var dobros = numeros.map(x => x * 2); 
console.log(dobros); //2, 10, 20, 30

Example working with the students:

const students = [
    { name: 'Anna', grade: 6 },
    { name: 'John', grade: 4 },
    { name: 'Maria', grade: 9 }
];

//byName agora com arrow function
console.log(students.map(x => x.name));

  • I have a habit of running the code on my part and trying to understand what happens, I had already understood how the map() worked, only I didn’t understand what was happening in this part of the code. Very good your reply, thank you indeed!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.