javascript - Accessing response data using Angular.js with numerical named field -


my response object has field called "50", right i'm trying access , save data doing this:

var thing = $scope.data.array[0].50;

however i'm getting error on console when reload page function not running. when rid of 50, fine. there indeed field called "50" inside $scope.data.array[0] , not have access change response. there wrong because field called "50" , maybe js interrupting number instead??

also when changed "50" random "af", no errors on refresh.

this doesn't work var thing = $scope.data.array[0].50;

this works var thing = $scope.data.array[0].af;

the following should work if first element of array has property called "50".

var thing = $scope.data.array[0]["50"];

property accessors provide access object's properties using dot notation or bracket notation.

syntax object.property object["property"] 

javascript objects associative arrays (hashes). using these can associate key string value string shown in example above.

the reason why don't error when accessing $scope.data.array[0].af; because "af" valid identifier property. dot notation works property names valid identifiers. identifier must start letter, $, _ or unicode escape sequence.

for other property names, must use bracket notation.


Comments