@@ -652,6 +652,8 @@ The only step remaining is to find a correlation for every type of
652
652
event that was recorded and see whether anything stands out. How
653
653
should we store these correlations once we compute them?
654
654
655
+ {{id for_of_loop}}
656
+
655
657
## Array loops
656
658
657
659
{{index "for loop", loop, [ array, iteration] }}
@@ -682,8 +684,8 @@ for (let entry of journal) {
682
684
683
685
When a ` for ` loops looks like this, with the word ` of ` after a
684
686
variable definition, it will loop over the elements of the value given
685
- after ` of ` . This works not only for arrays, but also for some other
686
- data structures. We'll discuss _ how_ it works in [ Chapter
687
+ after ` of ` . This works not only for arrays, but also for strings and
688
+ some other data structures. We'll discuss _ how_ it works in [ Chapter
687
689
6] ( 06_object.html ) .
688
690
689
691
## Objects as maps
@@ -812,7 +814,7 @@ for (let event of Object.keys(correlations)) {
812
814
// and so on...
813
815
```
814
816
815
- {{index "for/in loop "}}
817
+ {{index "Object.keys function "}}
816
818
817
819
Most correlations seem to lie close to zero. Eating carrots, bread, or
818
820
pudding apparently does not trigger squirrel-lycanthropy. It _ does_
@@ -1037,7 +1039,7 @@ It can be useful for a function to accept any number of ((argument))s.
1037
1039
For example, ` Math.max ` computes the maximum of _ all_ the arguments it
1038
1040
is given.
1039
1041
1040
- {{indexsee "period character", "max example"}}
1042
+ {{indexsee "period character", "max example", spread }}
1041
1043
1042
1044
To write such a function, you put three dots before the function's
1043
1045
last ((parameter)), like this:
@@ -1059,6 +1061,21 @@ array containing all further arguments. If there are other parameters
1059
1061
before it, their values aren't part of that array. But if it's the
1060
1062
first parameter, as in ` max ` , it will hold all arguments.
1061
1063
1064
+ {{index "function application"}}
1065
+
1066
+ You can use a similar three-dot notation to _ call_ a function with an
1067
+ array of arguments.
1068
+
1069
+ ```
1070
+ let numbers = [5, 1, 7];
1071
+ console.log(max(...numbers));
1072
+ // → 7
1073
+ ```
1074
+
1075
+ This "((spread))s" out the array into the function call, passing its
1076
+ elements as separate arguments. It is possible to include an array
1077
+ like that along with other arguments, as in ` max(9, ...numbers, 2) ` .
1078
+
1062
1079
## The Math object
1063
1080
1064
1081
{{index "Math object", "Math.min function", "Math.max function", "Math.sqrt function", minimum, maximum, "square root"}}
@@ -1197,12 +1214,14 @@ function phi([n00, n01, n10, n11]) {
1197
1214
This also works for ((binding))s created with ` let ` , ` var ` , or
1198
1215
` const ` . If you know the value you are binding is an array, you can
1199
1216
use ((square brackets)) to "look inside" of the value, binding its
1200
- content.
1217
+ content. This works well with ` Object.entries ` , for example.
1201
1218
1202
1219
```
1203
- let [a, b, c] = [1, 2, 3];
1204
- console.log(b);
1205
- // → 2
1220
+ for (let [name, value] in {x: 0, y: 1}) {
1221
+ console.log(name, value);
1222
+ }
1223
+ // → x 0
1224
+ // → y 1
1206
1225
```
1207
1226
1208
1227
{{index object, "curly braces"}}
@@ -1543,11 +1562,11 @@ compare properties only when _both_ arguments are objects. In all
1543
1562
other cases you can just immediately return the result of applying
1544
1563
` === ` .
1545
1564
1546
- {{index "for/in loop ", "in operator"}}
1565
+ {{index "Object.keys method ", "in operator"}}
1547
1566
1548
- Use a ` for ` / ` in ` loop to go over the properties. You need to test
1549
- whether both objects have the same set of property names and whether
1550
- those properties have identical values. The first test can be done by
1567
+ Use ` Object.keys ` to go over the properties. You need to test whether
1568
+ both objects have the same set of property names and whether those
1569
+ properties have identical values. The first test can be done by
1551
1570
counting the properties in both objects and returning false if the
1552
1571
numbers of properties are different. If they're the same, then go over
1553
1572
the properties of one object, and for each of them, verify that the
0 commit comments