forked from lgope/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloops.js
More file actions
99 lines (75 loc) 路 1.95 KB
/
Copy pathloops.js
File metadata and controls
99 lines (75 loc) 路 1.95 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const mil = 1000000;
const arr = Array(mil);
console.time('鈴诧笍');
for (let i = 0; i < mil; i++) {} // 1.6ms
for (const v of arr) {
} // 11.7ms
arr.forEach(v => v); // 2.1ms
for (let i = mil; i > 0; i--) {} // 1.5ms
console.timeEnd('鈴诧笍');
const equine = { unicorn: '馃', horse: '馃惔', zebra: '馃' };
for (const key in equine) {
// Filters out properties inherited from prototype
if (equine.hasOwnProperty(key)) {
console.log(equine[key]);
}
}
// Unwrap the the Values
for (const val of Object.values(equine)) {
console.log(val);
}
// Create a Map
const equine = new Map(Object.entries(equine));
for (const v of equine.values()) {
console.log(v);
}
const equine = [
['unicorn', '馃'],
['horse', '馃惔'],
['zebra', '馃'],
];
// 馃槖 Meh Code
for (const arr of equine) {
const type = arr[0];
const face = arr[1];
console.log(`${type} looks like ${face}`);
}
// 馃く Destructured Code
for (const [type, face] of equine) {
console.log(`${type} looks like ${face}`);
}
///////////////
arr[Symbol.iterator] = function () {
let i = 0;
let arr = this;
return {
next: function () {
if (i >= arr.length) {
return { done: true };
} else {
const value = arr[i] + '馃檳';
i++;
return { value, done: false };
}
},
};
};
////////////////////////////////
const faces = ['馃榾', '馃槏', '馃い', '馃く', '馃挬', '馃', '馃コ'];
// Transform values
const withIndex = faces.map((v, i) => `face ${i} is ${v}`);
// Test at least one value meets a condition
const isPoopy = faces.some(v => v === '馃挬');
// false
// Test all values meet a condition
const isEmoji = faces.every(v => v > '每');
// true
// Filter out values
const withoutPoo = faces.filter(v => v !== '馃挬');
// Reduce values to a single value
const pooCount = faces.reduce((acc, cur) => {
return acc + (cur === '馃挬' ? 1 : 0);
}, 0);
console.log(pooCount);
// Sort the values
const sorted = faces.sort((a, b) => a < b);