javascript vue get value in JSON

Viewed 579

I want to run through this JSON file and output the values under "states". e.g. Bayern - 220318. How can I access these values in a loop to show them in a list or table?

enter image description here

3 Answers

Use Object.keys() to iterate the object by keys.

Object.keys(output.states).forEach(key => {
  console.log(key, output.states[key].vaccinated);
});

If you want just the country names then you can define a computed property as below...

computed: {
 getStates() {
  return this.output.states; // assuming that you have stored the response in a variable called 'output' defined in the data section
 }
}

this is how you call the computed and print it in your template using v-for

<div v-for="(stateObject, state) in getStates" v-bind:key="state">
  <div>{{state}}</div> // to print the state name
  <div>{{stateObject.total}}</div> // to print the total value of that object
  <div>{{stateObject.vaccine}}</div> // to print the vaccine value of that object
 
</div>
Related