【问题标题】:JS/JQuery getting value of key in a multidimensional objectJS/JQuery 获取多维对象中键的值
【发布时间】:2015-12-30 02:03:51
【问题描述】:

我觉得真正阻碍我能力的一件事是了解如何有效地使用数组和对象。我掌握了基础知识,但是当涉及到在复杂对象中需要特定的键和值时,我迷路了。所以在这个例子中,我从 openweathermap 获取一个对象。对象如下所示:

{
    "coord":{
        "lon":-83.15,
        "lat":41.51
    },
    "weather":[
        {
            "id":721,
            "main":"Haze",
            "description":"haze",
            "icon":"50d"
        }
    ],
    "base":"cmc stations",
    "main":{
        "temp":287.05,
        "pressure":1024,
        "humidity":47,
        "temp_min":286.15,
        "temp_max":288.15
    },
    "wind":{
        "speed":5.1,
        "deg":50,
        "gust":9.3
    },
    "clouds":{
        "all":40
    },
    "dt":1443728852,
    "sys":{
        "type":1,
        "id":1435,
        "message":0.0115,
        "country":"US",
        "sunrise":1443699000,
        "sunset":1443741203
    },
    "id":5165215,
    "name":"Oak Harbor",
    "cod":200
}

我了解如何使用 each() 遍历数组

$.getJSON( "http://api.openweathermap.org/data/2.5/weather?zip=43452,us&APPID=6c62bbbc17614bb4c0cae3095e0b5a89", function(obj) {
    $.each(obj.main, function(key, val) {
        //do something//
    });
});

但是如果我想访问对象中更深层次的东西,比如说我特别想要温度值例如,然后将其分配给全局变量以在脚本外部使用,该怎么办。我想不需要一个具体的解决方案,但更多的是关于如何使用像这样有点复杂的对象的一个​​很好的可靠解释。

感谢您的帮助!

所以我现在有了这个,但是值没有从函数中传递出去?

$.getJSON( "http://api.openweathermap.org/data/2.5/weather?zip=43452,us&APPID=6c62bbbc17614bb4c0cae3095e0b5a89", function(obj) {
    currentTemp = (obj.main.temp * 9/5 - 459.67);
    alert(currentTemp);
});

alert(currentTemp);

【问题讨论】:

  • 一个对象就像一个字典,如果你需要一些特定的东西,只需通过它的键来解决它,比如obj.main.temp,如果你对同一个对象做一些事情,通常你会使用一个数组,尽管你可以使用它里面有不同的对象。这真的取决于你使用它的目的。

标签: javascript jquery arrays object multidimensional-array


【解决方案1】:
$.getJSON( "http://api.openweathermap.org/data/2.5/weather?zip=43452,us&APPID=6c62bbbc17614bb4c0cae3095e0b5a89", function(obj) {
    $.each(obj.main, function(key, val) {
        if(val.temp)
        {
            // do what you want with the temperature
        }
    });
});

【讨论】:

    【解决方案2】:

    你可以这样:

    $.getJSON( "http://api.openweathermap.org/data/2.5/weather?zip=43452,us&APPID=6c62bbbc17614bb4c0cae3095e0b5a89", function(obj) {
        $.each(obj.main, function(key, val) {
            your_global_var = val.temp;
        });
    });
    

    或者如果您想了解更多信息。

    $.getJSON( "http://api.openweathermap.org/data/2.5/weather?zip=43452,us&APPID=6c62bbbc17614bb4c0cae3095e0b5a89", function(obj) {
        $.each(obj, function(key, val) {
            your_global_var = val.main.temp;
        });
    });
    

    无论如何,我认为不需要$.each 循环,因为你可以这样:

    $.getJSON( "http://api.openweathermap.org/data/2.5/weather?zip=43452,us&APPID=6c62bbbc17614bb4c0cae3095e0b5a89", function(obj) {
            your_global_var = obj.main.temp;
    });
    

    【讨论】:

    • 所以没有每个循环说 $.obj, function(key, val) {} 没有每个?
    • 不,我的意思是 obj.main.temp 包含在函数中(obj){}
    猜你喜欢
    • 1970-01-01
    • 2016-07-26
    • 2017-02-15
    • 2018-12-12
    • 2021-08-25
    • 2015-11-19
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多