-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1016-array-3.html
85 lines (76 loc) · 2.18 KB
/
day1016-array-3.html
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
76
77
78
79
80
81
82
83
84
85
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="keywords" content="JavaScript,数组,数组高级方法">
<meta name="description" content="JavaScript 数组3">
<title>JavaScript 数组3</title>
<script>
var months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
console.log(months, "default sorted", months.slice().sort());
var shuffled = months.slice().sort((_, __) => Math.random() - 0.5);
console.log("shuffled", shuffled);
console.log("sorted", shuffled.slice().sort((a, b) => months.indexOf(a) - months.indexOf(b)));
var kvArray = [{ key: 1, value: 10 },
{ key: 2, value: 20 },
{ key: 3, value: 30 }];
console.log(
kvArray.map(pair => ({ [pair.key]: pair.value }))
);
var dirty = [
{ id: 15 },
{ id: -1 },
{ id: 0 },
{ id: 3 },
{ id: 12.2 },
{},
{ id: null },
{ id: NaN },
{ id: 'undefined' }
];
function isNumber(x) {
return typeof x === "number" && !Number.isNaN(x);
}
console.log(dirty.filter(i => isNumber(i.id) && i.id !== 0));
var numbers = new Array(10).fill().map(_ => Math.random());
console.log(numbers, "sum:", numbers.reduce((sum, x) => sum += x, 0));
var text = new Array(50).fill().map(_ => String.fromCharCode(97 + (122 - 97) * Math.random())).join('') + "@@@@";
console.log(text);
var counter = text.split('').reduce((cnter, ch) => {
if (cnter[ch] !== undefined) {
cnter[ch] += 1;
} else {
cnter[ch] = 1;
}
return cnter;
}, {});
console.log(counter);
var ancientText = "张三|男|李四|女|王五|男|赵柳|男|田七|女";
console.log(ancientText);
console.log(ancientText.split("|").filter((_, idx) => idx % 2 === 0));
console.log(ancientText.split("|").reduce((people, datum, idx) => {
if (idx % 2 === 0) {
people.push({});
people[people.length - 1].name = datum;
} else {
people[people.length - 1].gender = datum;
}
return people;
}, []));
</script>
</head>
<body></body>
</html>