I have a json array object like below

$scope.Json = [{ Id:"5464", Class:"9", Rank:"4" }]

I want to add a item "Name":"Vicky" to the Json. So that my result should be as below.

$scope.Json = [{ Id:"5464", Class:"9", Rank:"4", Name:"Vicky" }]

I am new to angular, can anyone help on this?

share|improve this question
    
Publicate of stackoverflow.com/questions/736590/… - please search before asking a new question. – lin Mar 28 at 15:47

First of all, the object $scope.Json is not a JSON but a string. To get a JSON, you need to parse the string like the following:

$scope.Json = JSON.parse(<string>) ;

Second, your input is a peculiar JSON as it is an array with one element (in its turn having 3 elements. I guess you wanted this:

$scope.Json = JSON.parse({ Id:"5464", Class:"9", Rank:"4" }) ;

Once you have this, you can add the element you want as:

$scope.Json.Name = "Vicky" ;
share|improve this answer

Use Array map() method.

DEMO

var json = [{ Id:"5464", Class:"9", Rank:"4" }];

json.map(function(item) {
  item.Name = 'Vicky'; 
});

console.log(json);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.