My current goal is to create a function called toggleStar, which will allow users to select an item from a list of JSON objects and add it to another list, known as the Favorite list.
I attempted to use indexOf in my code, but it doesn't seem to be functioning as expected. Any suggestions or help would be greatly appreciated!
Thank you in advance!
$scope.favlist = JSON.parse($window.localStorage.getItem("favlist"));
console.log($scope.favlist);
$scope.toggleStar = function(item) {
item.star = !item.star;
console.log(item);
var favlistcontent = $window.localStorage.getItem("favlist");
if(typeof favlistcontent !== 'string'){
$scope.favlist = [];
$window.localStorage.setItem("favlist",JSON.stringify($scope.favlist));
}
$scope.favlist = JSON.parse($window.localStorage.getItem("favlist"));
if( $scope.favlist.indexOf(item) === -1 ) {
$scope.favlist.push(item);
$window.localStorage.setItem("favlist",JSON.stringify($scope.favlist));
} else if( $scope.favlist.indexOf(item) > -1 ){
console.log("item already exist");
}
}