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

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

问题描述

JavaScript 中的 arguments 对象是一个奇怪的缺陷——它在大多数情况下就像一个数组,但它实际上不是一个数组对象.因为它是 完全是另外一回事,所以它没有'没有来自 Array 的有用功能.prototype 比如 forEachsortfiltermap.

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.

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

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();
}

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

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使用rest参数

如果你会使用 ES6,你可以使用:

ES6 using rest parameters

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();

您可以在 链接中阅读

其余参数语法允许我们将不定数量的参数表示为一个数组.

The rest parameter syntax allows us to represent an indefinite number of arguments as an array.

如果你对 ... 语法感到好奇,它被称为 Spread Operator,你可以阅读更多 这里.

If you are curious about the ... syntax, it is called Spread Operator and you can read more here.

使用 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 简单地将类数组或可迭代对象转换为数组实例.

Array.from simply convert Array-like or Iterable objects into Array instances.

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

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();
}

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

Why does this work? Well, here's an excerpt from the ECMAScript 5 documentation itself:

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

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.

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

Therefore, slice works on anything that has a length property, which arguments conveniently does.

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

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天全站免登陆