JavaScript中array.push的第二个参数是如何工作的?

How does the second parameter of array.push in JavaScript work?

我目前正在研究 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']

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

我感谢那些回答或评论我的 post 的人。我的 post 的主要目标是弄清楚 [ elementN]] 部分的含义,而不是 "copying array items into another array"。

我的例子

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

确实涉及到"array items into another array",但我只是想通过更多的例子来了解推送参数的一般规律。

在我提供的 link 的 MDN 网络文档页面上,显示的示例只有两个参数,例如

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 接收两个参数(事后证明这是一个错误的假设),这就是为什么我的问题主要是关于 "how the 'second parameter' [, elementN] works". 如果有更多的例子,比如

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 可以接受任意多的参数。

另一点是我不知道

...

也是代码的一部分,称为传播运算符。我只是认为你们是 'omitting' 那种表情的某些东西。这也导致了我的误解。

您可以使用展开运算符。

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

工作片段

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

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