gpt4 book ai didi

javascript - obj的长度是1 - javascript

转载 作者:行者123 更新时间:2023-12-02 15:32:47 24 4
gpt4 key购买 nike

我正在尝试根据用户插入的邮政编码自动填充城市和州值。我无法将输入的邮政编码与 json 对象中可用的邮政编码进行比较。 obj length 的值为 1 ,必须为 3。

var text = '{"zipcodes":[' +
'{"zip":"22031","city":"Fairfax" ,"State":"VA" },' +
'{"zip":"22032","city":"AshBurn" ,"State":"VA" },' +
'{"zip":"22033","city":"Tysons" ,"State":"VA" }]}';

var obj = JSON.parse(text);
var n = Object.keys(obj).length;

for(i=0; i<=n; i++){

if(obj.zipcodes[i].zip.parseInt() == zipcodes.parseInt()){

document.getElementById("city").value = obj.zipcodes[i].city;
document.getElementById("state").value = obj.zipcodes[i].State;

}
}

最佳答案

解析后的对象变为:

{
"zipcodes":[
{"zip":"22031","city":"Fairfax" ,"State":"VA" },
{"zip":"22032","city":"AshBurn" ,"State":"VA" },
{"zip":"22033","city":"Tysons" ,"State":"VA" }]}
]
};

所以对象的键数是 1,即邮政编码。如果您想要邮政编码的数量,

使用这个:

var obj = JSON.parse(text);
var n = obj.zipcodes.length;

关于javascript - obj的长度是1 - javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33195888/

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