-
Notifications
You must be signed in to change notification settings - Fork 1
/
array-methods-quokka.js
65 lines (49 loc) · 1.37 KB
/
array-methods-quokka.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
// creation
const a0 = new Array();
const a1 = [];
const a = [1, 2];
// there is more to this - in a few slides
// add
a.push(3); //?a
a.unshift(0); //?a
// remove
a.pop(); //?a
a.shift(); //?a
// get
const b = a[0]; //?a
// Exercise:
var some_languages = ["css", "html", "fortran"];
// Can you tell me, which of these array elements
// is the name of a language rarely (if ever?)
// known by JavaScript developers?
var rarely_known = undefined; // <-
// find
a.indexOf(2); //?
a.includes(2); //?
a.find((v) => v === 2); //?
a.findIndex((v) => v === 2); //?
a.filter((v) => v > 1); //?
// update
a[0] = 100; //?a
// spread
const [foo, bar] = a;
console.log("foo:", foo, "bar:", bar);
// reduce
const min = a.reduce((acc, val) => (acc < val ? acc : val));
console.log("min:", min);
const max = a.reduce((acc, val) => (acc > val ? acc : val));
console.log("max:", max);
const sum = a.reduce((acc, val) => acc + val);
console.log("sum:", sum);
const avg = a.reduce((acc, val, _index, { length }) => acc + val / length, 0);
console.log("avg:", avg);
// iterate
console.log("-- iterate --");
// a.map((v) => Math.pow(v,2)) //?
const _ = a.map((v) => v ** 2); //?
a.forEach((v, index) => console.log("foreach @", index, "=", v));
for (const v of a) {
console.log(":", v);
}
//...and there is more.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array