我正在嘗試使用變數來訪問我的 json 檔案中的欄位,我該怎么做這樣的事情?js:
let file = require('./file.json')
let someVar = 'firstField'
console.log(file.firstObject.{someVar})
json:
{
"firstObject": {
"firstField":"firstValue",
"secondField":"secondValue",
"thirdField":"thirdValue",
},
"otherObject": {
"otherField":"otherValue"
}
}
期望的結果 :
'firstValue'
uj5u.com熱心網友回復:
obect[str]就是你要找的。您的代碼應如下所示:
let file = require('./file.json')
let someVar = 'firstField'
console.log(file.firstObject[someVar])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/488899.html
標籤:javascript json
上一篇:如何使用reduce、map或filter創建一個函式來顯示和統計陣列物件的每個值?
下一篇:如何從外部HTML檔案訪問元素?
