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
| // Array.from(arrayLike)
Array.from('foo'); // ['f', 'o', 'o']
// Array.from(arrayLike, mapFn(element, index))
// 相等 Array.from().map()
Array.from([1, 2, 3], (x) => x + x) // [2, 4, 6]
Array.from([1, 2, 3]).map((x) => x + x);
const someNumbers = { '0': 10, '1': 15, length: 2 };
Array.from(someNumbers, value => value * 2); // => [20, 30]
// 轉換 Set
const set = new Set(["foo", "bar", "baz", "foo"]);
Array.from(set); // ['foo', 'bar', 'baz']
// 轉換 Map
const map = new Map([
[1, 2],
[2, 4],
[4, 8],
]);
Array.from(map);
// [[1, 2], [2, 4], [4, 8]]
const mapper = new Map([
["1", "a"],
["2", "b"],
]);
Array.from(mapper.values());
// ['a', 'b'];
Array.from(mapper.keys());
// ['1', '2'];
|