Array.from()静态方法从可迭代或类数组对象创建一个新的浅拷贝的数组实例。
1 2 3 4 5
| console.log(Array.from('foo'))
console.log(Array.from([1, 2, 3], x => x + x))
|
语法
1 2 3
| Array.from(arrayLike) Array.from(arrayLike, mapFn?) Array.from(arrayLike, mapFn?, thisArg?)
|
参数
arrayLike
想要转换成数组的可迭代或类数组对象
mapFn
(可选)
调用数组每个元素的函数。如果提供,每个添加到数组的值首先传递给该函数,然后将mapFn的返回值添加到数组中。该函数提供以下参数:
element
数组当前正在处理的元素
index
数组当前正在处理的元素的索引
thisArg
(可选)
执行mapFn时用作this的值
返回值
一个新的数组实例
描述
- 可迭代对象(例如map和set)
- 类数组对象(带有length属性和索引元素的对象)
Array.from不会创建稀疏数组,如果arrayLike缺少一些索引属性,那么这些属性在数组中将是undefined。
1 2
| console.log(Array.from({length: 5}))
|
示例
从字符串构建数组
从Set构建数组
1 2 3
| const set = new Set(["foo", "bar", "baz", "foo"]); Array.from(set);
|
从Map构建数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| const map = new Map([ [1, 2], [2, 4], [4, 8], ]); Array.from(map);
const mapper = new Map([ ["1", "a"], ["2", "b"], ]); Array.from(mapper.values());
Array.from(mapper.keys());
|
从NodeList创建数组
1 2 3 4 5 6 7 8 9 10 11 12 13
| const images = document.querySelectorAll("img"); console.log(images) const sources = Array.from(images, (image) => image.src); console.log(sources) const insecureSources = sources.filter((link) => link.startsWith("http://")); console.log(insecureSources)
|
从类数组构建数组(arguments)
1 2 3 4 5
| function test() { return Array.from(arguments) } test(1,2,3)
|
使用箭头函数和 Array.from()
1 2 3 4
| Array.from([1,2,3], (x) => x + x)
Array.from({length: 5}, (v, i) => i)
|
序列生成器range
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);
range(0, 4, 1);
range(1, 10, 2);
range("A".charCodeAt(0), "Z".charCodeAt(0), 1).map((x) => String.fromCharCode(x), );
|
在非数组构造函数上调用 from()
from() 方法可以在任何构造函数上调用,只要该构造函数接受一个表示新数组长度的单个参数。
1 2 3 4 5 6 7 8 9 10 11 12 13
| function NotArray(len) { console.log("NotArray called with length", len); }
console.log(Array.from.call(NotArray, new Set(["foo", "bar", "baz"])));
console.log(Array.from.call(NotArray, { length: 1, 0: "foo" }));
|
当 this 值不是构造函数,返回一个普通的数组对象。
1
| console.log(Array.from.call({}, { length: 1, 0: "foo" }));
|