gpt4 book ai didi

javascript - JQuery:从json读取值

转载 作者:行者123 更新时间:2023-12-02 16:23:18 25 4
gpt4 key购买 nike

我有以下代码:

var Configuration = {
conf : {},
buildConfig : function (){
var configugration = {};

configugration.supportedCountsInJSON = ["closedSRCount","resSRCount","openTAGCount","SECount"];
return configugration;
},

和一个像这样的变量:

var teamCountsJson = 
[ {
"associateId" : "AA029693",
"closedSRCount" : "10",
"resSRCount" : "8",
"openTAGCount" : "0",
"SECount" : "7",
},
{
"associateId" : "BB029693",
"closedSRCount" : "4",
"resSRCount" : "1",
"openTAGCount" : "0",
"SECount" : "1",
}]

当我尝试根据配置变量中的键读取上述 Json 中的值时,如下所示,我得到“未定义”:

calculateMinCounts: function(teamCountsJson){
$.each(teamCountsJson, function(index){
var minValues = [];
var sKey ='';
$.each(Configuration.conf.supportedCountsInJSON, function(id){
minValues = [];
sKey = Configuration.conf.supportedCountsInJSON[id]

if(teamCountsJson[index].hasOwnProperty(sKey)){
minValues.push(teamCountsJson[index].sKey);
console.log('sKey='+sKey+', minValues='+JSON.stringify(teamCountsJson[index].sKey)); //Unable to read the value 'undefined'
}
else{
//console.log('associateId does not exit!!'+Configuration.conf.supportedCountsInJSON[id]);
}

});

我知道

sKey = Configuration.conf.supportedCountsInJSON[id]

当我注销它们时,变量给了我以下键:

["closedSRCount","resSRCount","openTAGCount","SECount"];

但是,当我尝试读取上述代码中 Json 中每个键的值(例如,associateId)时,如下所示,我得到“未定义”并且无法读取这些值:

teamCountsJson[index].sKey

有什么想法吗?

请指教,

谢谢!

最佳答案

没有sKey属性,如果要使用包含属性名称的变量,则必须使用括号表示法

teamCountsJson[index][sKey]

关于javascript - JQuery:从json读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28909303/

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