Cannot read property 'splice' of undefined

Problem

I'm trying to remove the item from the localstorage json data. but have error in my console 'splice' undefined. Please Help Me..

This is my Controller

app.controller('favgame', function($scope, $localStorage){
    $scope.saved = localStorage.getItem('favgame');
    var favgames = JSON.parse($scope.saved);
    $scope.onItemDelete = function(index) {
      $scope.favgames.splice(favgames[index],1);
    };
});

This is my ng-click code

<ion-delete-button class="button button-clear ion-minus-circled" ng-click="onItemDelete($index)">
</ion-delete-button>

Thank You!

Problem courtesy of: Praveen JP

Solution

Incorrect splice, instead use below. $scope.favgames, doesn't contain any values and is undefined.

favgames.splice(index,1);

Or set,

$scope.favgames = favgames.splice(index,1);
Solution courtesy of: Mahesh Sapkal

Discussion

you have to change this line:

var favgames = JSON.parse($scope.saved);

with this one:

$scope.favgames = JSON.parse($scope.saved);
Discussion courtesy of: Fermín

Please change var favgames to var $scope.favgames

Discussion courtesy of: Ramesh Rajendran

This recipe can be found in it's original form on Stack Over Flow.