0

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.

4
  • Why do you JSON.stringify($scope.searched)? Isn't $scope.searched a string? Commented Jul 15, 2015 at 14:28
  • 2
    You don't show us how/where you read $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. Commented Jul 15, 2015 at 14:29
  • @Cerbrus : $scope.searched is an object. @meagar : I'm not sure I understood what you meant, I initialize $scope.savedSearch as an empty array, and I want my function to add the current $scope.searched object into this array. Commented Jul 15, 2015 at 14:31
  • much better ways to compare... you aren't accounting for angular hashkeys added to objects as well as you always push the same object reference Commented Jul 15, 2015 at 14:39

3 Answers 3

4

You'll want to change your push line to:

$scope.savedSearch.push(angular.copy($scope.searched));

I believe your problem is that objects are passed by reference. Since the object you have in the savedSearch is always pointing to the exact object you're searching, alreadyExist will always be true.

Sign up to request clarification or add additional context in comments.

Comments

2

My guess is that the object reference is being stored in your array, not the actual object itself. Because of this, any subsequent calls to push the object to your array will not work because the object reference already exists in the array. It's merely updated.

Try this instead. Use angular.copy() to create a deep copy of the object and push the copy to your array. See if that works.

if (!alreadyExist) {
  $scope.savedSearch.push(angular.copy($scope.searched));
  localStorage.setItem("savedSearch", JSON.stringify($scope.savedSearch));
}

1 Comment

Thanks man, resolved went to the first to post, you're getting an upvote though.
-1

You are pushing the Object outside of the for so only 1 element get pushed in try move it inside the for and every object which doesnt already exist will be pushed in

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

easier way would be to just

$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])) {
       $scope.savedSearch.push($scope.searched);
       localStorage.setItem("savedSearch", JSON.stringify($scope.savedSearch));
   }else{
     break
   }
 }
};

4 Comments

I think the point in that function is to push only 1 object. The loop is there to check if the object already exists (so it doesn't post duplicates)
well thats why there is the if statement so every object which is not already in there is getting pushed in
The for is there only to check the existence of $scope.searched in $scope.savedSearch array. Doing like you suggested would just push n times $scope.searched from what I see
why when it is pushed once it is always a duplicate!? After the first time it wont be pushed in since its duplicated right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.