-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
75 lines (54 loc) · 1.32 KB
/
map.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
///////////////////////
// ES6 - map helper
///////////////////////
/*
var numbers = [1,2,3];
var doubleNumbers = [];
// ES5 - for loop
for (var i = 0; i < numbers.length; i++) {
doubleNumbers.push(numbers[i] * 2);
}
console.log(doubleNumbers);
//ES6 - .map helper
var doubled = numbers.map(function(number) {
return number * 2;
});
console.log(doubled);
// =====================================
var cars = [
{ model: 'Buick', price: 'CHEAP' },
{ model: 'Camaro', price: 'expensive' }
];
// ES6 - .map
var prices = cars.map(function(car) {
return car.price;
});
console.log(prices);
*/
// =====================================
// Plucking values
var images = [
{ height: '34px', width: '39px' },
{ height: '54px', width: '19px' },
{ height: '83px', width: '75px' },
];
var heights = images.map(function(image){
return image.height;
});
// Calculating Values with Map
var trips = [
{ distance: 34, time: 10 },
{ distance: 90, time: 50 },
{ distance: 59, time: 25 }
];
var speeds = trips.map(function(trip) {
return trip.distance / trip.time;
});
console.log(speeds);
// Really Hard - Implementing 'Pluck'
var paints = [ { color: 'red' }, { color: 'blue' }, { color: 'yellow' }];
function pluck(paints, color) {
return paints.map(function(paint) {
return paint['color'];
});
}