javascript - 如何从 js 文件代码字符串(由 fs.readFileSync 获得)中获取部分 json?
问题描述
var a = {
properties:{
title:{
type:"String",
value:"sss"
},
content:{
type:"String",
value:"555"
}
},
data:{
abc:123,
ddd:444,
fff:"dd"
},
methods: {
abc () {
},
cde () {
}
}
}
上面的代码是来自 js 文件的文本字符串(由 fs.readFileSync 获取)。我需要通过正则表达式获取“属性”字符串的一部分并将其解析为 json 对象。
解决方案
您可以使用object Destructuring
const {properties} = a;
var a = {
properties: {
title: {
type: "String",
value: "sss"
},
content: {
type: "String",
value: "555"
}
},
data: {
abc: 123,
ddd: 444,
fff: "dd"
},
methods: {
abc() {
},
cde() {
}
}
}
const {properties} = a;
console.log(properties);
或使用dot
运营商访问
a.properties
或者
a["properties"]
推荐阅读
- django - Django View,获取模板中的所有列表项
- c# - 在ac#程序中,如何访问可观察集合的属性?
- java - mssql-jdbc-6.3.4.jre8-preview.jar 没有 sqljdbc_auth.dll
- spring-batch - 如何重新启动面向正确位置的弹簧批处理块?
- python-3.x - 将 csv 从 s3 转换为镶木地板的 Lambda 函数
- google-drive-api - Google Drive API - 在恢复原始文件夹之前恢复从垃圾箱恢复的文件的原始文件夹
- macos - 你可以在 MacOS 上安装 Powershell 模块吗?
- omnet++ - 计时器时间反映在仿真时间中
- python - 我如何看待 selenium webdriver 的输入?[Python]
- mysql - 导入数据库数据后,启用二进制日志记录只是擦除它