0

This is my spinet:

$scope.foodtypes = {
   'GRAINS': {
      'HOT': ['Rice','Beans','Sogum'],
      'ROOT': ['Yam','Potato','Cassava']
   }
}  

I like to store the exact word "GRAINS" or "ROOT" in a variable for decision making:

something like:

if foodtypes is "GRAINS" then ......

if foottypes is "ROOT" then ......

Please how do I capture "GRAIN" or "ROOT" for use?

0

1 Answer 1

-1

Assuming that foodtypes is a JSON string that represents an object that has all food types as first-level key, you could do something like this:

Object.keys(JSON.parse($scope.foodtypes));

This will return an array of strings ["GRAINS", "ROOT"].

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

3 Comments

Uncaught SyntaxError: Unexpected token o in JSON at position 1
"Unexpected token o in JSON at position 1" usually means that your data is already an object. So no need to do JSON.parse.
Thank you Kate, I am very grateful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.