Reputation: 59
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?
Upvotes: 0
Views: 4918
Reputation: 27192
Use Array map() method.
DEMO
var json = [{ Id:"5464", Class:"9", Rank:"4" }];
json.map(function(item) {
item.Name = 'Vicky';
});
console.log(json);
Upvotes: 2
Reputation: 3675
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" ;
Upvotes: 0