常规推送和Array.prototype.push.apply之间有什么区别

use*_*075 8 javascript apply javascript-objects

我不太明白以下两行代码之间的区别.在我的代码中,带有"apply"的行按照我想要的方式工作,而只有常规push的行不会.

那么当这两个都被执行时真正发生了什么

//this one does not work the way i want it to
$scope.items.push(result.data.stuff)

//this one works!
Array.prototype.push.apply($scope.items, result.data.stuff);
Run Code Online (Sandbox Code Playgroud)

编辑:抱歉混淆,我修复它,以便它有"推"方法

Dan*_*ite 13

新1.将数组推送到项目上.

$scope.items = [1, 2];
result.data.stuff = [3, 4];
$scope.items.push(result.data.stuff);
$scope.items[0] === 1;
$scope.items[1] === 2;
$scope.items[2][0] === 3;
$scope.items[2][1] === 4;
Run Code Online (Sandbox Code Playgroud)

旧1.删除现有的引用$scope.items.

$scope.items = [1, 2];
result.data.stuff = [3, 4];
$scope.items = result.data.stuff;
$scope.items[0] === 3;
$scope.items[1] === 4;
Run Code Online (Sandbox Code Playgroud)

2.将所有项目推result.data.stuff$scope.items,保留现有项目.

$scope.items = [1, 2];
result.data.stuff = [3, 4];
Array.prototype.push.apply($scope.items, result.data.stuff);
$scope.items[0] === 1;
$scope.items[1] === 2;
$scope.items[2] === 3;
$scope.items[3] === 4;
Run Code Online (Sandbox Code Playgroud)


Krz*_*cze 9

Function.prototype.apply()解释了基本区别。

你可以在那里阅读:

您可以使用 push 将元素附加到数组。而且,因为 push 接受可变数量的参数,您还可以一次推送多个元素。

但是,如果你传递一个数组来推送,它实际上会将该数组添加为 单个元素,而不是单独添加元素。所以你 最终得到了一个数组中的一个数组。

所以如果你做这样的事情:

let numbersArray = [1, 2]
numbersArray.push([3, 4])
Run Code Online (Sandbox Code Playgroud)

您将在数组中有一个数组:

[1, 2, [3,4]]
Run Code Online (Sandbox Code Playgroud)

当您有要添加的变量列表时,可以使用 push.apply()

let numbersArray = [1, 2]
numbersArray.push.apply(numbersArray, [3, 4])
Run Code Online (Sandbox Code Playgroud)

然后您的结果将如下所示:

[1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)


exd*_*xd4 7

Array.prototype.push()是一种方法,它将一个或多个元素添加到数组的末尾并返回数组的新长度. Array.prototype.push.apply()获取原始数组和包含要添加到原始数组的元素的数组.

示例Array.prototype.push():

var numbers = [1, 5, 2, 8];
numbers.push(3, 4, 6, 7);
console.log(numbers); // [1, 5, 2, 8, 3, 4, 6, 7]
Run Code Online (Sandbox Code Playgroud)

Array.prototype.push()嵌套数组的示例:

var foods = [['apples', 'pears']];
foods.push(['lettuce', 'celery']);
console.log(foods); // [['apples', 'pears'], ['lettuce', 'celery']]
Run Code Online (Sandbox Code Playgroud)

示例Array.prototype.push.apply():

var grades = [90, 88, 83, 85];
var more_grades = [79, 84, 81, 90];
Array.prototype.push.apply(grades, more_grades);
console.log(grades); // [90, 88, 83, 85, 79, 84, 81, 90]
Run Code Online (Sandbox Code Playgroud)

Array.prototype.push.apply()嵌套数组的示例:

var sports = [['running', 'cycling']];
var other_sports = [['football', 'basketball']];
Array.prototype.push.apply(sports, other_sports);
console.log(sports);
// [['running', 'cycling'], ['football', 'basketball']]
Run Code Online (Sandbox Code Playgroud)

参考文献:

应用


epa*_*llo 5

push()将为您传入的每个参数添加一个索引。它不关心它添加到数组中的内容。你告诉添加它的每一个都会将它添加到数组的末尾。

当您使用apply() 时,它会将您提供的数组作为第二个参数并将其转换为多个参数。MDN 解释得很好,但基本上把它变成了

yourArray.push(argument[0],argument[1],argument[2],argument[3]);
Run Code Online (Sandbox Code Playgroud)