-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_map.js
More file actions
42 lines (32 loc) · 1.13 KB
/
1_map.js
File metadata and controls
42 lines (32 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(function(){
let people = [
{"name": "Suyogya", "age": 18},
{"name": "Suyash", "age": 8},
{"name": "Sulav", "age": 18},
{"name": "Nabin", "age": 19}
];
/*
From the people array of object extract all the people's name
You pass the function to map method that returns array name
But how does that work?
*/
let getNames = function(n) {
return n.name;
}
people = people.map(getNames);
/*
Do you see that getNames is a function that takes a parameter named n and returns name attribute of it?
When map is called on people it passes every element individually to getNames function and makes an
array of element of whatever that is returned by the isAdult function.
Basically this is what map is doing:
function map(func){
new_arr = [];
for(let i = 0; i<people.length; i++){
let element = func(people[i]);
new_arr.push(element);
}
return new_arr;
}
*/
console.log(people);
})();