从对象中删除项目

时间:2017-09-19 02:31:42

标签: javascript angularjs arrays oop

我真的在努力从我的物体中移除物品。该对象具有随机生成的键,因此它使其变得更难,但我无法使用splice从对象中删除该项...而且我不确定为什么......

这是我的目标:$scope.todos.trackers

这就是$scope.todos的样子:

enter image description here

这就是对象的trackers部分:

enter image description here

因此,为了尝试删除tracker对象中的一个项(例如最后一项`note:“finally”,value:200),这就是我的代码:

function removeIndividualTracker(uid, item) {
    angular.forEach($scope.todos.trackers, function(key, value) {
        angular.forEach(key, function(el, val) {
            console.log(key)
            console.log(item)
            if(key == item) {
                console.log($scope.todos)
                console.log($scope.todos.trackers)
                $scope.todos.trackers.splice($scope.todos.trackers.indexOf($scope.todos.trackers[value]), 1);
            }
        });
    });
}

但它不起作用。我收到$scope.todos.trackers.indexOf is not a function的错误。

有什么想法吗?我真的不知道该怎么办..谢谢!

2 个答案:

答案 0 :(得分:1)

您收到该错误是因为跟踪器不是数组而是对象。尝试这样的事情......

function removeIndividualTracker(key) {
     delete $scope.todos.trackers[key];             
}

答案 1 :(得分:0)

您收到此错误是因为tracker是一个对象。如果tracker是一个对象数组,您可以使用indexOf。您可以从此处阅读更多相关信息:MDN indexOf

现在回到你的问题,你可以删除:

$itemId = trim($valPartsDetail['Id']); 
$productGrpName = trim($valPartsDetail['productGroupId']); 
$length = null;
$width = null;
$height = null;
$weight = null;
foreach ($valPartsDetail['attributes'] as $key => $attrVal) 
{
  ...

您可以从此处了解有关删除的详情:MDN delete

相关问题