我怎么能转换"参数"反对在JavaScript中的数组? [英] How can I convert the "arguments" object to an array in JavaScript?
问题描述
在JavaScript中的参数
对象是一个奇怪的疣,它的行为就像在大多数情况下一个数组,但它不是的真正的数组对象。由于它是真的别的东西完全,它没有有用从<一个功能href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype\"><$c$c>Array.prototype$c$c>像的forEach
,排序
,过滤
和地图
。
这是很轻松构建一个新的数组从参数用一个简单的for循环对象。例如,该功能排序它的参数:
函数sortArgs(){
变参= [];
对于(VAR I = 0; I&LT;与arguments.length;我++)
ARGS [I] =参数[I]
返回args.sort();
}
不过,这是一个相当可怜的东西都简单地做去的非常有用的JavaScript数组功能的访问。是否有一个内置的方式使用标准库做呢?
您可以实际只使用阵列
的<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice\"><$c$c>slice$c$c>在一个参数的函数对象,并且将其转换成一个标准的JavaScript数组。你只需要通过Array的原型手动引用它:
函数sortArgs(){
变参= Array.prototype.slice.call(参数);
返回args.sort();
}
为什么这项工作?嗯,这里是从的ECMAScript 5文档本身的摘录:
注意:在
片
功能是有意通用的;它不要求它的这个值是一个数组对象。因此它可以被转移到其他类型的对象,其用作方法。无论是片
功能可以成功地应用到主机对象是依赖于实现的。
块引用>因此,
片
适用于任何有长度
属性,参数
方便呢。如果
Array.prototype.slice
是太多你一口,你可以稍微用数组字面缩写是:变参= [] .slice.call(参数);
不过,我倾向于认为,以前的版本更加明确,所以我preFER它来代替。滥用阵列文字符号感觉哈克和看起来怪怪的。
如果您能够使用ES6你可以使用:
<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters\">Rest参数
\r\r\r\r函数sortArgs(参数... args){\r
返回args.sort(功能(A,B){返回一个 - B;});\r
}\r
\r
document.body.innerHTML = sortArgs(12,图4,6,8)的ToString();\r
正如你可以在<读href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters\">link
其余参数语法让我们重新present参数不确定数量为数组。
块引用>如果你是好奇
...
语法,它被称为的取值$ P $垫操作的,你可以阅读更多的<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/S$p$pad_operator\">here.<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from\">Array.from
\r\r\r\r函数sortArgs(){\r
返回Array.from(参数)的.sort(功能(A,B){返回 - B;});\r
}\r
\r
document.body.innerHTML = sortArgs(12,图4,6,8)的ToString();\r
Array.from
简单地转换阵列状或可迭代的对象到数组实例。不幸的是,
Array.from
和恢复参数目前只在Firefox和谷歌浏览器的新版本实现(检查浏览器兼容性表上面的链接),但你可以使用一个像transpiler 通天以efficently写ES6 code具有非常低的兼容性问题。The
arguments
object in JavaScript is an odd wart—it acts just like an array in most situations, but it's not actually an array object. Since it's really something else entirely, it doesn't have the useful functions fromArray.prototype
likeforEach
,sort
,filter
, andmap
.It's trivially easy to construct a new array from an arguments object with a simple for loop. For example, this function sorts its arguments:
function sortArgs() { var args = []; for (var i = 0; i < arguments.length; i++) args[i] = arguments[i]; return args.sort(); }
However, this is a rather pitiful thing to have to do simply to get access to the extremely useful JavaScript array functions. Is there a built-in way to do it using the standard library?
解决方案You can actually just use
Array
'sslice
function on an arguments object, and it will convert it into a standard JavaScript array. You'll just have to reference it manually through Array's prototype:function sortArgs() { var args = Array.prototype.slice.call(arguments); return args.sort(); }
Why does this work? Well, here's an excerpt from the ECMAScript 5 documentation itself:
NOTE: The
slice
function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method. Whether theslice
function can be applied successfully to a host object is implementation-dependent.Therefore,
slice
works on anything that has alength
property, whicharguments
conveniently does.
If
Array.prototype.slice
is too much of a mouthful for you, you can abbreviate it slightly by using array literals:var args = [].slice.call(arguments);
However, I tend to feel that the former version is more explicit, so I'd prefer it instead. Abusing the array literal notation feels hacky and looks strange.
If you are able to use ES6 you can use:
function sortArgs(...args) { return args.sort(function (a, b) { return a - b; }); } document.body.innerHTML = sortArgs(12, 4, 6, 8).toString();
As you can read in the link
The rest parameter syntax allows us to represent an indefinite number of arguments as an array.
If you are curious about the
...
syntax, it is called Spread Operator and you can read more here.
function sortArgs() { return Array.from(arguments).sort(function (a, b) { return a - b; }); } document.body.innerHTML = sortArgs(12, 4, 6, 8).toString();
Array.from
simply convert Array-like or Iterable objects into Array instances.
Unfortunately,
Array.from
and Rest Parameters are currently only implemented in newer versions of Firefox and Google Chrome (check the Browser compatibility table in the above links ) but you can use a transpiler like Babel to efficently write ES6 code with really low compatibility issues.这篇关于我怎么能转换&QUOT;参数&QUOT;反对在JavaScript中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!