gpt4 book ai didi

javascript - key 中包含 "-"的 Json

转载 作者:行者123 更新时间:2023-12-03 08:11:51 25 4
gpt4 key购买 nike

var data = {
"train": {
"number": "12155",
"days": [
{
"runs": "Y",
"day-code": "SUN"
},
{
"runs": "Y",
"day-code": "MON"
},
{
"runs": "Y",
"day-code": "TUE"
},
{
"runs": "Y",
"day-code": "WED"
},
{
"runs": "Y",
"day-code": "THU"
},
{
"runs": "Y",
"day-code": "FRI"
},
{
"runs": "Y",
"day-code": "SAT"
}
],
"name": "BHOPAL EXPRESS"
},
"response_code": 200
};
$(document).ready(function(){

var datalist = data.train.days;


for(var i in datalist)
{
$(".train").append("<p> Run on" +datalist[i].day-code +"-" + datalist[i].runs +"</p>");
}


});

这段代码给我一个错误 datalist[i].day-code “代码未定义”。我如何访问包含“-”的 key 。如果我只使用 datalist[i].runs 它运行正常.请提出建议。

最佳答案

您需要更换

datalist[i].day-code

datalist[i]['day-code']

关于javascript - key 中包含 "-"的 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34106794/

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