0

Here is my current json response.

[{"id":1,"name":"Yangon","$$hashKey":"object:170"}]

But I want to add new node to above json as follow,

[{"id":1,"name":"Yangon","$$hashKey":"object:170", "township_id": 1, "township_name": "Ahlone"}]

Please help me how to do it. Thanks in advance.

1
  • @naomik - It's not? (If you assume it's a string, it is valid JSON. Though it doesn't really make sense to add properties to JSON: I'd parse it and manipulate the resulting object...) Commented Jun 29, 2016 at 4:09

3 Answers 3

1
var arrayofobjects=[{"id":1,"name":"Yangon","$$hashKey":"object:170"}]
arrayofbojects[0].township_id: 1;
arrayofbojects[0].township_name: "Ahlone";

console.log(arrayobjectg[0])

try this it is tested code.

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

2 Comments

This has syntax errors. How and where did you test it ?
sorry console.log(arrayofobjects[0])
1

angular.extend is another way of doing this.

var arr = [{ "id": 1, "name": "Yangon", "$$hashKey": "object:170" }];
arr[0] = angular.extend(arr[0], {
    "township_id": 1,
    "township_name": "Ahlone"
});

2 Comments

Don't forget the original data exists in an array
@naomik Sorry about that. Corrected.
0

You need to store your response into a variable (it probably already is) and just add whatever key/values you want:

Example

var arr = [{ "id": 1, "name": "Yangon", "$$hashKey": "object:170" }];

arr[0]['township_id'] = 1;
arr[0]['township_name'] = 'Ahlone';

Result

[{"id":1,"name":"Yangon","$$hashKey":"object:170","township_id":1,"township_name":"Ahlone"}]

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.