0

I'm trying to add new value into existing JSON object in AngularJS but I'm getting this error message:

"Object doesn't support property or method 'push'"

Here is my code:

$scope.addStatus = function (text) {          
          $scope.application.push({ 'status': text }); //I have tried 'put' but getting the error
      };

 $scope.create = function( application ){
        $scope.addStatus('Under review');
 }

here is my application json looks like:

{"type":"Not completed","source":"mail","number":"123-23-4231","amount":"234.44","name":"John ","id":"123","by_phone":true}

I want to append/add status to the above json and something looks like after adding the status property:

{"type":"Not completed","source":"mail","number":"123-23-4231","amount":"234.44","name":"John ","id":"123","by_phone":true, "status": "under review"}
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • Where is $scope.application defined? If its not an array, then your code will fail. – sma Oct 08 '14 at 17:20
  • @psl: how come you tag this is as duplicate? care to explain? – Nick Kahn Oct 08 '14 at 17:28
  • 2
    Because if you read the answer you will know. It is just worded differently. You should atleast put some effort to find out what the error message means, to start with (rather than pasting the error from the console in the question)... I am sorry i don't think there is no way it is not a duplicate. Hope this explains. – PSL Oct 08 '14 at 17:31

2 Answers2

3

The .push() is a method of an JS Array.

But application is just an object (not an Array). Here we can just use:

// instead of this
// $scope.application.push({ 'status': text });
// use this
$scope.application.status = text;
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
1

Array.prototype.push is only for arrays. Add the property in one of the following manners:

$scope.application['status'] = text;

Or:

$scope.application.status = text;
Alex
  • 34,899
  • 5
  • 77
  • 90