-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmethods-examples.ts
63 lines (49 loc) · 1.15 KB
/
methods-examples.ts
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
const array = [1, 2, 3, 4];
const object = {
a: 1,
b: 2,
c: "asdf",
};
// ==========================
// COPY
// ==========================
const copy1 = {
...object,
};
// mutates 1st object
const copy2 = Object.assign({}, object);
const copy3 = [...array];
const copy4 = array.slice();
const copy5 = array.concat([]);
function immutableCopyArray<T>(array: T[]) {
const newArray: T[] = [];
for (let i = 0, l = array.length; i < l; i++) {
newArray[i] = array[i];
}
return newArray;
}
function immutableCopyObject<T>(object: Record<string, T>) {
const newRecord = {};
for (const key in object) {
newRecord[key] = object[key];
}
return newRecord;
}
// ==========================
// iterate
// ==========================
const filtered = array.filter((item) => item > 0);
const mapped = array.map((item) => item * 1);
Object.keys(object).forEach((key) => {
console.log(key, object[key]);
});
Object.values(object).forEach((value) => {
console.log(value);
});
Object.entries(object).forEach(([key, value]) => {
console.log(key, value);
});
for (const key in object) {
const value = object[key];
console.log(key, value);
}