gpt4 book ai didi

javascript - Angular JS - Array.splice() 导致未定义的项目

转载 作者:行者123 更新时间:2023-11-28 07:21:58 25 4
gpt4 key购买 nike

美好的一天!

我目前正在通过使用 Push 方法在索引 0 处添加对象并使用 splice 方法删除项目来操作我的数组。正如我所阅读和理解的,当您从数组中拼接一个项目时,它不会让数组留下“未定义”的项目。问题是,我目前在使用拼接时收到“未定义”项目。

这是我添加条目的代码:

addOrRemoveRating(0,0,{
rating : 0,
tran_number : transaction_number,
email : $scope.called_numbers[0].email
});

这是我用于删除条目的代码:

 addOrRemoveRating(array_index,1);

其中 array_index 是现有索引。

最后一部分是拼接发生的地方:

addOrRemoveRating = function(index, item, object){
$scope.temp_called_numbers.splice(index, item, object);
}

例如,我的数组中有3个对象 - [Object, Object, Object],删除该项目后,它返回 - [Object, Object, undefined]。

我的代码有遗漏或错误的地方吗?非常感谢任何帮助、引用或指导。

最佳答案

当您仅使用两个参数调用函数时,object将是undefined ,因此,undefined将添加到数组中。

试试这个

$scope.temp_called_numbers.splice.apply($scope.temp_called_numbers, arguments);

JSFiddle

使用它,您可以利用 Array.prototype.splice 的可变参数性质并传递多个对象以添加到数组中。

关于javascript - Angular JS - Array.splice() 导致未定义的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30205770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com