I have a function like this :
$scope.saveSearch = function () {
var alreadyExist = false;
for (var i = 0; i < $scope.savedSearch.length; i++) {
if (JSON.stringify($scope.searched) === JSON.stringify($scope.savedSearch[i])) {
alreadyExist = true;
break;
}
}
if (!alreadyExist) {
$scope.savedSearch.push($scope.searched);
localStorage.setItem("savedSearch", JSON.stringify($scope.savedSearch));
}
};
Before that : $scope.savedSearch = [];
$scope.searched = {
IS: "",
area: "",
block: "",
type: "",
level: ""
};
The values in $scope.searched object are initialized and then modified by the user.
My problem is :
$scope.savedSearch always contains only the last pushed object. Instead of adding the object to the array, it just replaces the current object.
I don't understand why.
JSON.stringify($scope.searched)? Isn't$scope.searcheda string?$scope.savedSearch, you just tell is that it is[]before the code you run. As far as we can tell, it's doing exactly what you're telling it to do.$scope.searchedis an object. @meagar : I'm not sure I understood what you meant, I initialize$scope.savedSearchas an empty array, and I want my function to add the current$scope.searchedobject into this array.