I have an array like this:
["Tom","John"]
And I want to convert it to the following json format:
["nameList"]:[{"name":"Tom"},{"name":"John"}]
How to achieve this?
You can run a loop
on the existing object, specifying the json data for the new AngularJS object as:
var data = ["Tom", "John"];
$scope.angularData = {
'nameList': []
};
angular.forEach(data, function(v, k) {
$scope.angularData.nameList.push({
'name': v
});
});
Watch the demo.
{"nameList":[{"name":"Tom"},{"name":"John"}]}