Array.from与Array.prototype.map [英] Array.from vs Array.prototype.map
问题描述
那么这两个函数有什么区别?
So what is the difference between this two functions?
它们都创建 new Array
对象。到目前为止我发现的唯一区别是 Array.from
支持 ArrayLike
参数。我没有看到为什么他们没有添加 ArrayLike
支持 Array.prototype.map
函数的任何理由。
They both create new Array
object. Only difference I found so far is that Array.from
supports ArrayLike
parameters. I don't see any reason why they just haven't added ArrayLike
support for Array.prototype.map
function.
我错过了什么吗?
推荐答案
<$的目的c $ c> Array.from()是取一个非数组(但类似于数组)的对象,并将其复制到一个实际的数组中。然后,这允许您在副本上使用所有数组方法,包括除了迭代之外的其他内容,例如 .splice()
, .sort()
, .push()
, .pop()
等...这显然是多少能够胜任 .map()
使用类似数组的东西。
The purpose of Array.from()
is to take a non-array (but array-like) object and make a copy of it into an actual array. This then allows you to use ALL array methods on the copy including things beyond just iterating it such as .splice()
, .sort()
, .push()
, .pop()
, etc... which is obviously much more capable than just make .map()
work with array-like things.
这篇关于Array.from与Array.prototype.map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!