Py学习  »  Jquery

jquery对数组的处理不当:length=0错误?

davide m. • 4 年前 • 243 次点击  

我不知道这是我对jquery的一点了解,还是它只是一个bug,但下面是发生的情况。我有一小段json代码


{
    "planes":[
        {
            "id":1,
            "name":"Boeing 767-300",
            "height":54.9 ,
            "wingspan":47.6, 
            "vel": 851,
            "vel max":913,
            "plane width":283.3,
            "weight":86070, 
            "full weight":158760, 
            "passengers":{
                "1 class":350,
                "2 class":269,
                "3 class":218
            },
            "fuel tank":90.625,
            "engine":"2 turbofan General Electric CF6-80C2"
        },
        {
            "id":2,
            "name":"Boeing 737-800",
            "height":33.4 ,
            "wingspan":35.8, 
            "vel": 840,
            "vel max":945,
            "plane width":105.44,
            "weight":32704, 
            "full weight":56472, 
            "passengers":{
                "1 class":189
            },
            "fuel tank":90.625,
            "engine":"2 turbofan CFM56-3C1"
        }
    ]
}

然后我会用jquery的 getJSON 没有任何瑕疵。然后我想要两个独立的数组:一个保存键,另一个保存值,同样没有问题 Object.keys Object.values 。通过将结果记录在一个字符串中,一切都很好。直到我尝试构造一个关联数组,使用键作为索引,使用值作为数据。通过记录结果,我得到一个值为“0”的额外“length”索引。这是我的jquery代码


var arr=[];
$.getJSON("js/jsondata.json", function(data){
    var keys= Object.keys(data.planes[0]);
    var values= Object.values(data.planes[0]);
//im only testing on the first object, for now

    $.each(keys, function(i){
//creating the associative index and assigning the value
        arr[keys[i]]= values[i];
        console.log("Key: "+ keys[i]+", Value: "+values[i]);
//this logs the exact values and indexes
    });
    console.log(arr);
//this logs an extra "length" 0
});
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46250
 
243 次点击  
文章 [ 2 ]  |  最新文章 4 年前