Reputation: 23
I have a Json schema like this:
{"THEMES":{
"CATEGORY":
["TEST1","TEST2","TEST3","DFSDFSDF"],
"OVER AGAIN":
["YOUNG","OLD","GRANPA"],
"AND AGAIN":
["AND","ANOTHER","WORD"],
"NEW CATEGORY":
["AGAIN","OOPS","CAN","REDBULL"]
}}
Well, I need to retrieve the keys values inside an AngularJs Controller and my code to retrieve it is like:
$http.get('json/word_bank.json')
.success(function (result) {
$scope.themes = Object.keys(result.TEMAS);
console.log($scope.themes);
And I get in my console: ["CATEGORY", "OVER AGAIN", "AND AGAIN", "NEW CATEGORY"]
So far so good, but I need to access the keys values like TEST1, TEST2, etc. For this i've tried, for example: console.log($scope.themes[0][0]);
And the console return the letter "C" or the first letter of the "CATEGORY" string. And this is happening with all of the rest, so the code is transforming my initial array into string elements, I guess. I already tried a lot of loops or iterations over the array, but I not works. Am I missing something?
Upvotes: 0
Views: 43
Reputation: 2588
$scope.themes
Is referring to an array of the object keys in the result.THEMES
object. When you call $scope.themes[0][0]
, you are getting the first item of the keys array "CATEGORY", and then the first character of that string "C".
You will need a reference to the actual themes object, then pass the name of the theme to get its values.
themesObject[$scope.themes[0]] -> ["TEST1","TEST2","TEST3","DFSDFSDF"]
Using your code:
$http.get('json/word_bank.json')
.success(function(result) {
$scope.themes = Object.keys(result.THEMES);
console.log(result.THEMES[$scope.themes[0]]);
});
Upvotes: 1