javascript Array.of和Array.from

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Array.of和Array.from相关的知识,希望对你有一定的参考价值。

// Array.of will take all passed arguments and turn into an array

Array.of('One', 2, 'Three', 4);
// ['One', 2, 'Three', 4]

// Array.from will take anything array-like / iterable and it into an array

Array.from({ length: 4 });
// [undefined, undefined, undefined, undefined]

// Array.from also accepts map functions
Array.from({ length: 4 }, x => 'Hello');
// [Hello, Hello, Hello, Hello]

const animals = new Set();

animals.add('Dog');
animals.add('Cat');
animals.add('Hamster');

animals
// Set(3) {"Dog", "Cat", "Hamster"}

Array.from(animals);
// ["Dog, "Cat", "Hamster"]

以上是关于javascript Array.of和Array.from的主要内容,如果未能解决你的问题,请参考以下文章

javascript ES6 - Array.of()

ES6---JavaScript

Array javascript

前端八股文——javascript中Array⽅法总结

Array.of() && Array()

数组对象新增方法Array.from()Array.of()