gpt4 book ai didi

javascript - 从json中的动态键值获取数据

转载 作者:可可西里 更新时间:2023-11-01 01:45:18 25 4
gpt4 key购买 nike

要求如下:
我必须从页面获取位置字段。

var input= global.input = document.getElementById("Location");

根据输入从json文件中获取邻域区域并显示在页面上。

我有一个 json 对象,必须根据键值(位置)从 json 对象中过滤数据

var inputLocation=input.value;

在我的 javascript 中,如果我使用动态 key ,我会收到错误消息。

如果我这样做 data.Aspen,我可以得到 json 数组,但我必须从文本字段中获取数据,而且它可能不同,所以如果我调用 data.inputLocation.. . 它即将到来 undefined

当我使用 data.(inputLocation.value) 时出现以下错误:

XML filter is applied to non-XML value ({Aspen:[{ID:

{
"Aspen":[
{
"ID":"Bellaire",
"Name":"Bellaire"
},
{
"ID":"Champions Forest",
"Name":"Champions Forest"
},
{
"ID":"Highland Village",
"Name":"Highland Village"
},
{
"ID":"Museum District",
"Name":"Museum District"
}
]
}

最佳答案

您可以使用类似数组的语法访问该属性:

data[inputLocation]

如果inputLocation设置为"Aspen",那么和这两行是一样的:

data["Aspen"]
data.Aspen

关于javascript - 从json中的动态键值获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3863198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com