我想遍历这个JSON文件,并在“state”下输出值。例如拜仁- 220318。如何在循环中访问这些值以在列表或表中显示它们?

发布于 2021-01-20 17:41:06
如果您只想要国家名称,那么您可以定义一个计算属性,如下所示...
computed: {
getStates() {
return this.output.states; // assuming that you have stored the response in a variable called 'output' defined in the data section
}
}这就是调用计算结果并使用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>发布于 2021-01-20 16:58:02
使用Object.keys()按键迭代对象。
Object.keys(output.states).forEach(key => {
console.log(key, output.states[key].vaccinated);
});发布于 2021-01-20 17:30:07
for (const [key, value] of Object.entries(object1)) {
console.log(`${key}: ${value}`);
}
你可以阅读更多:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
https://stackoverflow.com/questions/65806096
复制相似问题