【问题标题】:How does the second parameter of array.push in JavaScript work?JavaScript中array.push的第二个参数是如何工作的?
【发布时间】:2019-06-05 13:35:44
【问题描述】:

我目前正在研究 Array.prototype.push() 如何在 MDN 网络文档上工作。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

在此页面上,通用语法表示为

arr.push(element1[, ...[, elementN]])

,但是第二个参数(elementN)的作用是什么?

此页面显示了一个添加不同类型运动的示例,例如

var sports = ['soccer', 'baseball'];
var total = sports.push('football', 'swimming');

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total);  // 4

,但是如果你想执行以下操作,第一个和第二个参数是什么?

var teamSports = ['soccer', 'baseball', 'hockey', 'American football'];
var individualSports = ['weight lifting', 'track & field', 'boxing', 'wrestling']

// prepare an empty array
var allSports = []; 

// add all of the team sports 
allSports.push(???????)

// add all of the individual sports
allSports.push(????????)

// all the sports added to the array
console.log(allSports); // ['soccer', 'baseball', 'hockey', 'American football', 'weight lifting', 'track & field', 'boxing', 'wrestling']

[下面的附加背景(在得到一些答案和评论之后)]

我感谢那些回答或评论我的帖子的人。我的帖子的主要目标是弄清楚 [, elementN]] 部分的含义,而不是“将数组项复制到另一个数组中”。

我的例子

allSports.push(???????) 

确实涉及到“将数组项放入另一个数组”,但我只是试图通过获取更多示例来找出推送参数的一般规则。

在我提供链接的 MDN web 文档页面上,显示的示例只有两个参数,例如

var sports = ['soccer', 'baseball'];
var total = sports.push('football', 'swimming');

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total);  // 4

var vegetables = ['parsnip', 'potato'];
var moreVegs = ['celery', 'beetroot'];

// Merge the second array into the first one
// Equivalent to vegetables.push('celery', 'beetroot');
Array.prototype.push.apply(vegetables, moreVegs);

console.log(vegetables); // ['parsnip', 'potato', 'celery', 'beetroot']

由于这两个示例都有两个参数,我只是假设 push 接收两个参数(事后看来这是一个错误的假设),这就是为什么我的问题主要是关于“第二个参数如何' [, elementN] 有效”。 如果有更多的例子,比如

var sports = ['soccer', 'baseball'];
var total = sports.push('football', 'swimming');

console.log(sports); // ['soccer', 'baseball', 'football', 'swimming']
console.log(total);  // 4

total = sports.push('weight lifting', 'track & field', 'boxing', 'wrestling')
console.log(sports); // ['soccer', 'baseball', 'football', 'swimming', 'weight lifting', 'track & field', 'boxing', 'wrestling']
console.log(total); // 8

,我不会假设 push 需要两个参数,而 [, elementN] 是它的第二个参数,我会理解 push 可以使用任意数量的参数。

另外一点是我不知道

...

也是称为扩展运算符的代码的一部分。我只是觉得你们用这种表达方式“省略”了一些东西。这也导致了我的误解。

【问题讨论】:

  • elementN 不是 second 参数,它是 last - 您可以添加任意数量的元素。例如allSports.push(...individualSports)
  • @jonrsharpe 感谢您的评论。我在原始帖子中添加了更多背景信息。
  • @Ivar 感谢您的评论。我在原始帖子中添加了更多背景信息。

标签: javascript arrays push


【解决方案1】:

您可以使用扩展运算符。

allSports.push(...teamSports,...individualSports);

工作sn-p

var teamSports = ['soccer', 'baseball', 'hockey', 'American football'];
var individualSports = ['weight lifting', 'track & field', 'boxing', 'wrestling']
var allSports = []; 
allSports.push(...teamSports,...individualSports);
console.log(allSports)

【讨论】:

  • 感谢您的解释。所以 allSports.push(...teamSports) 等价于 allSports.push('soccer', 'baseball', 'hockey', 'American football') 对吧?
  • @ShinichiTakagi 绝对是。
【解决方案2】:

Array.push() 接受多个参数,因此当您想要合并数组时应使用spread 运算符。

像这样:

var teamSports = ['soccer', 'baseball', 'hockey', 'American football'];
var individualSports = ['weight lifting', 'track & field', 'boxing', 'wrestling']

// prepare an empty array
var allSports = [];

// add all of the team sports 
allSports.push(...teamSports)

// add all of the individual sports
allSports.push(...individualSports)

// all the sports added to the array
console.log(allSports);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2013-07-06
    • 2017-05-02
    • 1970-01-01
    • 2019-08-05
    • 2023-03-19
    • 2017-12-12
    相关资源
    最近更新 更多