我如何转换“参数"?对象到 JavaScript 中的数组? [英] How can I convert the "arguments" object to an array in JavaScript?

查看:19
本文介绍了我如何转换“参数"?对象到 JavaScript 中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript 中的arguments 对象是一个奇怪的疣——它在大多数情况下就像一个数组,但它实际上不是一个数组对象.由于它是完全不同的东西,它不会没有来自 Array 的有用功能.prototypeforEachsortfiltermap.

使用简单的 for 循环从参数对象构造一个新数组非常容易.例如,这个函数对它的参数进行排序:

function sortArgs() {var args = [];for (var i = 0; i < arguments.length; i++)args[i] = 参数[i];返回 args.sort();}

然而,为了访问极其有用的JavaScript数组函数而不得不这样做是一件相当可怜的事情.是否有使用标准库的内置方法?

解决方案

ES6 使用 rest 参数

如果您能够使用 ES6,您可以使用:

休息参数

function sortArgs(...args) {return args.sort(function (a, b) { return a - b; });}document.body.innerHTML = sortArgs(12, 4, 6, 8).toString();

正如您在链接中所读到的那样

<块引用>

rest 参数语法允许我们将无限数量的参数表示为一个数组.

如果您对 ... 语法感到好奇,它被称为 Spread Operator,您可以阅读更多此处.

ES6 使用 Array.from()

使用Array.from:

function sortArgs() {return Array.from(arguments).sort(function (a, b) { return a - b; });}document.body.innerHTML = sortArgs(12, 4, 6, 8).toString();

Array.from 简单地将类数组或可迭代对象转换为数组实例.

<小时><小时>

ES5

您实际上可以只使用 Arrayslice 对参数对象执行函数,并将其转换为标准的 JavaScript 数组.您只需要通过 Array 的原型手动引用它:

function sortArgs() {var args = Array.prototype.slice.call(arguments);返回 args.sort();}

为什么会这样?好吧,这是 ECMAScript 5 文档本身的摘录:

<块引用>

注意:slice 函数是故意通用的;它不要求它的 this 值是一个 Array 对象.因此,它可以转移到其他类型的对象以用作方法.slice 函数能否成功应用于宿主对象取决于实现.

因此,slice 适用于任何具有 length 属性的东西,arguments 很方便.

<小时>

如果 Array.prototype.slice 对你来说太多了,你可以使用数组字面量稍微缩写:

var args = [].slice.call(arguments);

不过,我倾向于觉得前一个版本更明确,所以我更喜欢它.滥用数组字面量让人感觉很笨拙,而且看起来很奇怪.

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 from Array.prototype like forEach, sort, filter, and map.

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?

解决方案

ES6 using rest parameters

If you are able to use ES6 you can use:

Rest Parameters

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.

ES6 using Array.from()

Using Array.from:

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.



ES5

You can actually just use Array's slice 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 the slice function can be applied successfully to a host object is implementation-dependent.

Therefore, slice works on anything that has a length property, which arguments 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.

这篇关于我如何转换“参数"?对象到 JavaScript 中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆