英文:
How make query in JSON with variable substitution
问题
I have JSON file like this (example):
{
"deecracks": [
{
"layercodename": "osm",
"layername": "Open Street Map"
},
{
"layercodename": "olen",
"layername": "tracks"
}
],
"theboomtownrats": [
{
"layercodename": "kuku",
"layername": "Open Street Map"
},
{
"layercodename": "olen",
"pathto": "rerere.json"
}
}
}
I can get the value I need:
var megatest = data.theboomtownrats[0].layercodename;
This will be kuku.
But theboomtownrats or deecracks or other values like this are contained in a variable. If I write just data.theboomtownrats[0].layercodename in the code, everything works. However, if I do something like this:
var testmega = 'data.' + ${artist} + '[0].layercodename';
or just concatenate text and a variable, I get the same string in the variable testmega.
Is it possible to substitute the value of a variable in the query? If possible, how?
p.s. I found that this can be done using eval(), and it works, but people say it's not safe and should be done using a function, but I'm having trouble with this.
英文:
I have JSON file like this (example):
{
"deecracks": [
{
"layercodename": "osm",
"layername": "Open Street Map",
},
{
"layercodename": "olen",
"layername": "tracks",
}
],
"theboomtownrats": [
{
"layercodename": "kuku",
"layername": "Open Street Map",
},
{
"layercodename": "olen",
"pathto": "rerere.json",
}
]
}
I can get value what me need:
var megatest = data.theboomtownrats[0].layercodename;
This will be kuku
But theboomtownrats or deecracks or other value like this contain in variable.
If i in code write just data.theboomtownrats[0].layercodename all is work, but if i do like this:
var testmega = `'data.' + ${artist} + '[0].layercodename'`
or just use concatenation text and variable i get just same string in var testmega.
Its possible - substitution value of variable in query? If possible - how?
p.s. I found - this can make over the eval() and this work, but people say it's not safe, and must do over the function, but i have failute with this.
答案1
得分: 1
你可以使用 JavaScript 对象的方括号表示法并向它们传递一个变量。
所以,不需要用句点分隔,只需将字符串包装在方括号中。然后,你可以在方括号中传递一个变量。
let artist = "deecracks";
console.log(data[artist][0]["layercodename"])
let data = {
"deecracks": [{
"layercodename": "osm",
"layername": "Open Street Map",
},
{
"layercodename": "olen",
"layername": "tracks",
}
],
"theboomtownrats": [{
"layercodename": "kuku",
"layername": "Open Street Map",
},
{
"layercodename": "olen",
"pathto": "rerere.json",
}
]
}
let artist = "deecracks";
console.log(data[artist][0]["layercodename"])
请注意,这是你提供的代码的翻译。
英文:
You can use bracket notation with javascript objects and pass a variable to them.
So instead of separating with a period, just wrap the string in brackets. And you can pass a variable in the brackets.
let artist = "deecracks";
console.log(data[artist][0]["layercodename"])
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let data = {
"deecracks": [{
"layercodename": "osm",
"layername": "Open Street Map",
},
{
"layercodename": "olen",
"layername": "tracks",
}
],
"theboomtownrats": [{
"layercodename": "kuku",
"layername": "Open Street Map",
},
{
"layercodename": "olen",
"pathto": "rerere.json",
}
]
}
let artist = "deecracks";
console.log(data[artist][0]["layercodename"])
<!-- end snippet -->
答案2
得分: 1
尝试使用方括号表示法。像这样:
var artist = 'theboomtownrats';
var megatest = data[artist][0].layercodename;
英文:
Try the bracket notation. Like this:
var artist = 'theboomtownrats';
var megatest = data[artist][0].layercodename;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论