0

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?

1

2 Answers 2

2

Use Array map() method.

DEMO

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

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

console.log(json);

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

Comments

0

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" ;

Comments

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.