Javascript 什么';s常规推送和Array.prototype.push.apply之间的区别

Javascript 什么';s常规推送和Array.prototype.push.apply之间的区别,javascript,javascript-objects,apply,Javascript,Javascript Objects,Apply,我不太明白下面两行代码之间的区别。在我的代码中,带有“apply”的行按我所希望的方式工作,而带有常规推送的行则不行 那么,当这两项都被执行时,到底发生了什么: //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); 编辑:很抱

我不太明白下面两行代码之间的区别。在我的代码中,带有“apply”的行按我所希望的方式工作,而带有常规推送的行则不行

那么,当这两项都被执行时,到底发生了什么:

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

编辑:很抱歉造成混淆,我已将其修复,因此其中包含“推送”方法。这会将数组推送到项目上

$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;
老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;
二,。将
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;
我想
$scope.items=result.data.stuff
不等于
Array.prototype.push.apply($scope.items,result.data.stuff)

因为第一次重新分配数组(清除旧元素)

试试这个:

$scope.items.push(result.data.stuff[0], result.data.stuff[1], ...);

$scope.items.push.apply($scope.items,result.data.stuff)

上面的on等于call Array.prototype.push.apply,因为$scope.items是一个数组(我们认为是)

还有一个joiner函数:

$scope.items = $scope.items.concat(result.data.stuff);
push()
将为传入的每个参数添加一个索引。它不关心向数组添加什么。您每次告诉它添加的内容都会将其添加到数组的末尾

使用时,它会将您提供的数组作为第二个参数,并将其转换为多个参数,但它基本上把它变成了

yourArray.push(argument[0],argument[1],argument[2],argument[3]);
Array.prototype.push()
是一种将一个或多个元素添加到数组末尾并返回数组新长度的方法
Array.prototype.push.apply()

Array.prototype.push()示例

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

var foods = [['apples', 'pears']];
foods.push(['lettuce', 'celery']);
console.log(foods); // [['apples', 'pears'], ['lettuce', 'celery']]
var sports = [['running', 'cycling']];
var other_sports = [['football', 'basketball']];
Array.prototype.push.apply(sports, other_sports);
console.log(sports);
// [['running', 'cycling'], ['football', 'basketball']]
Array.prototype.push.apply()示例

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

var foods = [['apples', 'pears']];
foods.push(['lettuce', 'celery']);
console.log(foods); // [['apples', 'pears'], ['lettuce', 'celery']]
var sports = [['running', 'cycling']];
var other_sports = [['football', 'basketball']];
Array.prototype.push.apply(sports, other_sports);
console.log(sports);
// [['running', 'cycling'], ['football', 'basketball']]
参考资料:

见:

请注意本页的注释:

注意:虽然此函数的语法与call()的语法几乎相同,但基本区别在于call()接受参数列表,而apply()接受单个参数数组

apply()
方法使用给定的
此值和作为数组(或类似数组的对象)提供的参数调用函数。argsArray(第二个参数)中的所有项都将按顺序用于
Array.prototype.push
。这与:

$scope.items.push.apply($scope.items,result.data.stuff)

因为
$scope.items.push==Array.prototype.push
apply()
接受类似数组的参数,但接受参数列表


简单地说,
apply
会将类似数组的参数转换为该函数的片段。

上解释了基本区别

你可以在那里阅读:

可以使用push将元素附加到数组中。而且,因为推 接受可变数量的参数,也可以推送多个参数 元素同时出现

但是,如果您将一个数组传递给push,它实际上会将该数组添加为 单个元素,而不是单独添加元素。所以你 以数组中的数组结束。

所以,如果你这样做:

let numbersArray = [1, 2]
numbersArray.push([3, 4])
您将在阵列中拥有一个阵列:

[1, 2, [3,4]]
当您有一个要添加的变量列表时,可以使用push.apply()

那么您的结果将如下所示:

[1, 2, 3, 4]

没有“定期推”的路线?是的,我很确定,
$cope.items.push(result.data.stuff[0],results.data.stuff[1],…)
会起作用。很抱歉搞混了……我修复了它。请再次查看推送行。很抱歉,格式不正确,似乎存在标记错误。或者您只是没有使用8个空格进行缩进。;)fixed@DeepakA你什么意思?是的,我就是这个意思。推送(result.data.stuff)。但是它没有按照我想要的方式工作:items.push(result.data.stuff)将整个result.data.stuff数组作为一个项放入items中,但是array.prototype.push.apply的第二个参数是一个参数数组,以便将每个元素插入$scope.items中$scope.items=$scope.items.concat(result.data.stuff);也合并了两个阵列,请保持这样的帖子英语哈哈!我是新来的,我的英语可能有点差poor@BergiWell,我们的中文(?)更差…看上面的中文是对之前的英语的简单描述。这就是答案。非常感谢。
[1, 2, 3, 4]