javascript - 无法访问对象内的键
问题描述
我正在尝试访问 Javascript 对象内的键的值。我的对象目前看起来像:
const options = {
"account.country": getCountry,
"account.phone": getPhone,
}
当 I 时console.log(options)
,它显示整个对象。但是,当我尝试
console.log(options.account) // undefined,
console.log(options.account.country) // error.
我不明白为什么。我也试过:
const parsedObj = JSON.parse(options);
console.log(parsedObj);
但它只是返回
'位置 1 的 JSON 中的意外标记 o'
解决方案
当您想从字符串访问属性时,您应该使用括号表示法。
const options = {
"account.country": 'getCountry',
"account.phone": 'getPhone',
}
console.log(options['account.country'])
推荐阅读
- javascript - 在 node.js 中将 rejectUnauthorized 与 node-fetch 一起使用
- python - FileNotFoundError 尝试使用数据集 20_newspaper 的朴素贝叶斯算法
- r - 在 R 中使用更长的枢轴来堆叠多列
- css - 制作
- 像标准一样行事
使用引导程序 4 列 - 像标准一样行事
- mongodb - 如何在 $facet 中返回输入文档
- r - 在ggplot2中,ggplot函数和first geom函数中包含的美学映射有什么区别?
- php - PhpSpreadsheet 公式未在保存的电子表格中运行
- gradle - 如何在 Gradle 6.1.1 中使用 Gradle Kotlin DSL 运行命令行命令?
- firebase - Flutter Cloud Firestore 不会自动更新
- javascript - 如何在不使用 jquery 的情况下获取标题标签?