我认为如果您想使用上述内容像数字一样访问它,您可以执行以下操作,我做了同样的事情并且工作了。
var myObj = {"0":"a","1":"b","CNT":2};
$.each(myObj,function(key,value){
if(isNaN(parseInt(key))){
return true; //continue;
}
//Code to perform operation
}
这仅在键不以数字字符开头时才有效,否则它将被转换为数字。请参见以下示例:
parseInt("45kjghk") === 45
我在这里使用了 jQuery
更新:
var myObj = {"0":"a","1":"b","CNT":2};
$.each(myObj,function(key,value){
if(isNaN(parseInt(key)) || (key.length !== parseInt(key).toString().length) ){
return true; //continue;
}
//Code to perform operation
}
它可以克服上述问题。如果可用,请提出更好的建议,如果有,请提出这个答案的问题。