Estoy tratando de usar una variable para acceder a un campo en mi archivo json, ¿cómo hago algo como esto? 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" } }resultado deseado :
'firstValue'object[str] es lo que está buscando. Su código debería verse así:
let file = require('./file.json') let someVar = 'firstField' console.log(file.firstObject[someVar])