0

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?

1
  • you don't have a 0 index at the top of your array. arr['THEMES']['CATEGORY'][1] would bring back TEST2 Commented Aug 20, 2015 at 15:13

1 Answer 1

1

$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]]);
  });
Sign up to request clarification or add additional context in comments.

1 Comment

Worked like a charm. Thank you very much.

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.