Simon
Simon

Reputation: 2538

Remove item from object

I'm really struggling removing an item from my object. The object has randomly generated keys so it makes it a little harder, but I can't remove the item from the object using splice... and I'm not sure why..

This is my object: $scope.todos.trackers

This is what $scope.todos looks like:

enter image description here

And this is what the trackers part of the object looks like:

enter image description here

So in order to try and delete one of the items in the tracker object (for instance the last item `note: "finally", value: 200), this is what my code looks like:

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

but it doesn't work. I get an error of $scope.todos.trackers.indexOf is not a function.

Any ideas? I really don't know what to do.. Thanks!

Upvotes: 0

Views: 80

Answers (2)

amrender singh
amrender singh

Reputation: 8239

You are getting this error because tracker is an object.You could have used indexOf if tracker was an array of objects.You can read more about it from here : MDN indexOf

Now coming back to your question, you can delete by :

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

You can read more about delete from here: MDN delete

Upvotes: 0

PurpleOrange
PurpleOrange

Reputation: 86

You are getting that error because tracker is not an Array but rather an Object. Try something like this...

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

Upvotes: 1

Related Questions