Javascript Angular JS-Array.splice()导致未定义项

Javascript Angular JS-Array.splice()导致未定义项,javascript,angularjs,array-splice,Javascript,Angularjs,Array Splice,你好 我目前正在使用push方法在索引0处添加对象,并使用splice方法删除项,从而操纵数组。正如我所阅读和理解的,当您从数组拼接一个项时,它不会在数组中留下“未定义”项。问题是,当使用我的接头时,我当前得到一个“未定义”项 以下是我添加条目的代码: addOrRemoveRating(0,0,{ rating : 0, tran_number : transaction_number, email : $scope.called_numbers[0].email })

你好

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

以下是我添加条目的代码:

addOrRemoveRating(0,0,{
    rating : 0,
    tran_number : transaction_number,
    email : $scope.called_numbers[0].email
});
 addOrRemoveRating(array_index,1);
以下是我删除条目的代码:

addOrRemoveRating(0,0,{
    rating : 0,
    tran_number : transaction_number,
    email : $scope.called_numbers[0].email
});
 addOrRemoveRating(array_index,1);
其中数组_索引是现有索引

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

addOrRemoveRating = function(index, item, object){
    $scope.temp_called_numbers.splice(index, item, object);
}
例如,我的数组中有3个对象-[Object,Object,Object],删除该项后,它返回-[Object,Object,undefined]


我的代码有什么遗漏或错误吗?非常感谢您提供的任何帮助、参考或指导。

当您仅使用两个参数调用函数时,
对象
将是
未定义的
,因此,
未定义的
将添加到数组中

addOrRemoveRating = function(index, item, object){
    if (object)
        $scope.temp_called_numbers.splice(index, item, object);
    else
        $scope.temp_called_numbers.splice(index, item);
}
试试这个

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


使用此方法,您可以利用
Array.prototype.splice
的varargs特性,并传递多个对象以添加到数组中。

这不是AngularJS的问题,而是严格的Javascript。使用三个参数调用
splice()
,将在指定位置拼接第三个参数。如果不使用第三个参数(
object
),它只会从数组中删除对象

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

谢谢,我会试试看哪个更有效。谢谢菲尔。你的解释帮助了我,但为了简单起见,我将遵循Brent的建议。抱歉,我真的认为这是一个角度问题,因为最近我遇到了很多JQuery角度问题。我还在学习角度。我将对此进行测试。即使我没有指定第三个参数,我也会看到这种情况发生