首页 > 解决方案 > 如何使用变量从 json 中取出对象?

问题描述

我有一个小问题,我无法理解。

let content = getData() //This will return a json in the variable content
content[0] //This is the output that i want but instead of [0], I want to store this into a variable

let path = "[0]"
content.path //something like that but the good way, can't figure it out how i could do this

谢谢!

标签: javascript

解决方案


尝试使用括号表示法,因为这允许动态值作为键名:

let path = "0";
content[path]

推荐阅读