0

I've got a JSON array i'm loading from localstorage which I want to add more items to and put back in local storage. However, i'm getting a bit lost and an error saying concat doesn't exist.

 $scope.searchObj = {
      term: searchTerm
    };

    $scope.curObj = $scope.curObj.concat($scope.searchObj);

    localStorage.setObject('searchObj', $scope.curObj);

$scope.curObj currently looks like:

Object {term: "fs"}

And I'd like to push the searchTerm (in searchObj) in to curObj so it looks like

{"term":"fs","term":"searchterm"}
2
  • I would convert it into an array, then merge both arrays and then convert it back into an object. Commented Jan 5, 2017 at 16:20
  • the thing you are missing is You are not converting it into array push works with only Array so ... simple thing missing Commented Jan 5, 2017 at 16:25

2 Answers 2

1

$scope.searchObj is not an array but a JSON object.

You actually want to merge two objects. You can use in plain JS :

for (var attrname in $scope.curObj) { 
       $scope.searchObj[attrname] = $scope.curObj[attrname]; 
     }

Or by using angular extend

$scope.searchObj = angular.extend($scope.searchObj, $scope.curObj);
Sign up to request clarification or add additional context in comments.

1 Comment

For future users: angular.extend does not support recursive deep copy - use angular.merge for that.
0
  1. Use ngStorage for storing object into localstorage and use service $localStorage - Reference

  2. Use angular.extend(oldobject,newobject) for merging

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.