-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreduceExample.js
More file actions
58 lines (43 loc) · 2 KB
/
reduceExample.js
File metadata and controls
58 lines (43 loc) · 2 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//As a next exercise, we will cover a basic example using reduce().
var a = [1,5,6,4,7,9,3,1,4,6,1,4];
/*
Suppose we wanted to sum all the elements of this array. Once again, we could accomplish this using a for() loop, but ultimately, we would like to use the reduce() function.
*/
//Using a for() loop
var sum = 0;
for(var i=0;i<a.length;i++){
sum += a[i];
}
console.log('The sum of the elements of this array is: ' + JSON.stringify(sum));
//Using reduce
var sigma = function(accumulator,newItem){
return accumulator + newItem;
};
var newSum = a.reduce(sigma,0);
console.log('Using reduce, the sum of the elements of the same array is: ' + JSON.stringify(newSum));
/*
Notice that reduce(), like map(), requires a function to be passed to it as the first argument. However, we can also give reduce a "starting value" by passing a second argument. In this case we wanted our sum to start from zero, but in general, the starting value can be any number. Another difference between map() and reduce() is that map() creates a new array, of the same size, from an existing one, while reduce() simply goes through the elements of a specified array, one by one.
*/
/*
So far, so good, but what if we wanted to add objects to construct a new object instead of the elements of an array?
As an example, let's add the Cartesian unit vectors to construct a new vector.
*/
var unitVectors = {
e1: {x:1,y:0,z:0},
e2: {x:0,y:1,z:0},
e3: {x:0,y:0,z:1}
};
/*
var vecSum = function(vectors){
var xComponent = vectors.e1.x + vectors.e2.x + vectors.e3.x;
var yComponent = vectors.e1.y + vectors.e2.y + vectors.e3.y;
var zComponent = vectors.e1.z + vectors.e3.z + vectors.e3.z;
return {x: xComponent, y: yComponent, z: zComponent};
};
var newVector = unitVectors.reduce(vecSum);
console.log('x: ' + JSON.stringify(newVector.x) + ', y: ' + JSON.stringify(newVector.y) + ', z: ' + JSON.stringify(newVector.z));
*/
var first = function(object){
return Object.keys(object)[0];
};
console.log(first(unitVectors));