在javascript中过滤json数组

时间:2016-10-01 21:16:52

标签: javascript json filter

我正在从气象服务中获取Json数据。我需要一个JavaScript函数来为每个dt_txt过滤掉一个最高main.temp的数组。

示例:我有5天的数据,每天有8条记录。我需要每天获得一个温度最高的记录。任何帮助将受到高度赞赏。

[{    "dt":1475355600,
 "main":{  
        "temp":294.22,
        "temp_min":294.22,
        "temp_max":295.309,
        "pressure":1009.36,
        "sea_level":1021.34,
        "grnd_level":1009.36,
        "humidity":52,
        "temp_kf":-1.09
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01n"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":1.27,
        "deg":285.503
    },
    "sys":{  
        "pod":"n"
    },
    "dt_txt":"2016-10-01 21:00:00"
                },
{  
    "dt":1475366400,
    "main":{  
        "temp":293.49,
        "temp_min":293.49,
        "temp_max":294.306,
        "pressure":1009.32,
        "sea_level":1021.42,
        "grnd_level":1009.32,
        "humidity":57,
        "temp_kf":-0.82
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01n"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":1.22,
        "deg":349.001
    },
    "sys":{  
        "pod":"n"
    },
    "dt_txt":"2016-10-02 00:00:00"
},
{  
    "dt":1475377200,
    "main":{  
        "temp":293.13,
        "temp_min":293.13,
        "temp_max":293.672,
        "pressure":1011.03,
        "sea_level":1023.03,
        "grnd_level":1011.03,
        "humidity":61,
        "temp_kf":-0.55
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01d"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":1.42,
        "deg":6.50092
    },
    "sys":{  
        "pod":"d"
    },
    "dt_txt":"2016-10-02 03:00:00"
},
{  
    "dt":1475388000,
    "main":{  
        "temp":300.82,
        "temp_min":300.82,
        "temp_max":301.096,
        "pressure":1012.55,
        "sea_level":1024.46,
        "grnd_level":1012.55,
        "humidity":72,
        "temp_kf":-0.27
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01d"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":2.06,
        "deg":10.5145
    },
    "sys":{  
        "pod":"d"
    },
    "dt_txt":"2016-10-02 06:00:00"
},
{  
    "dt":1475398800,
    "main":{  
        "temp":304.139,
        "temp_min":304.139,
        "temp_max":304.139,
        "pressure":1011.34,
        "sea_level":1023.24,
        "grnd_level":1011.34,
        "humidity":70,
        "temp_kf":0
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01d"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":2.96,
        "deg":15.0044
    },
    "sys":{  
        "pod":"d"
    },
    "dt_txt":"2016-10-02 09:00:00"
},
{  
    "dt":1475409600,
    "main":{  
        "temp":305.239,
        "temp_min":305.239,
        "temp_max":305.239,
        "pressure":1009.96,
        "sea_level":1021.78,
        "grnd_level":1009.96,
        "humidity":52,
        "temp_kf":0
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01d"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":4.06,
        "deg":21.503
    },
    "sys":{  
        "pod":"d"
    },
    "dt_txt":"2016-10-02 12:00:00"
},


{  
    "dt":1475560800,
    "main":{  
        "temp":300.429,
        "temp_min":300.429,
        "temp_max":300.429,
        "pressure":1012.43,
        "sea_level":1024.37,
        "grnd_level":1012.43,
        "humidity":74,
        "temp_kf":0
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01d"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":2.1,
        "deg":292.503
    },
    "sys":{  
        "pod":"d"
    },
    "dt_txt":"2016-10-04 06:00:00"
},
{  
    "dt":1475571600,
    "main":{  
        "temp":304.116,
        "temp_min":304.116,
        "temp_max":304.116,
        "pressure":1011.18,
        "sea_level":1023,
        "grnd_level":1011.18,
        "humidity":70,
        "temp_kf":0
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01d"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":2.21,
        "deg":316.5
    },
    "sys":{  
        "pod":"d"
    },
    "dt_txt":"2016-10-04 09:00:00"
},
{  
    "dt":1475582400,
    "main":{  
        "temp":305.353,
        "temp_min":305.353,
        "temp_max":305.353,
        "pressure":1009.66,
        "sea_level":1021.39,
        "grnd_level":1009.66,
        "humidity":49,
        "temp_kf":0
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01d"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":2.39,
        "deg":346.503
    },
    "sys":{  
        "pod":"d"
    },
    "dt_txt":"2016-10-04 12:00:00"
},
{  
    "dt":1475593200,
    "main":{  
        "temp":300.609,
        "temp_min":300.609,
        "temp_max":300.609,
        "pressure":1009.85,
        "sea_level":1021.74,
        "grnd_level":1009.85,
        "humidity":45,
        "temp_kf":0
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01n"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":1.68,
        "deg":16.0005
    },
    "sys":{  
        "pod":"n"
    },
    "dt_txt":"2016-10-04 15:00:00"
},
{  
    "dt":1475604000,
    "main":{  
        "temp":296.264,
        "temp_min":296.264,
        "temp_max":296.264,
        "pressure":1010.76,
        "sea_level":1022.71,
        "grnd_level":1010.76,
        "humidity":51,
        "temp_kf":0
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01n"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":1.48,
        "deg":85.0018
    },
    "sys":{  
        "pod":"n"
    },
    "dt_txt":"2016-10-04 18:00:00"
},
{  
    "dt":1475614800,
    "main":{  
        "temp":294.28,
        "temp_min":294.28,
        "temp_max":294.28,
        "pressure":1010.2,
        "sea_level":1022.3,
        "grnd_level":1010.2,
        "humidity":56,
        "temp_kf":0
    },
    "weather":[  
       {  
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01n"
       }
    ],
    "clouds":{  
        "all":0
    },
    "wind":{  
        "speed":1.27,
        "deg":123.501
    },
    "sys":{  
        "pod":"n"
    },
    "dt_txt":"2016-10-04 21:00:00"
}]

1 个答案:

答案 0 :(得分:0)

您可以使用哈希表作为日期,并在迭代中获取最大值。

var data = [{ dt: 1475355600, main: { temp: 294.22, temp_min: 294.22, temp_max: 295.309, pressure: 1009.36, sea_level: 1021.34, grnd_level: 1009.36, humidity: 52, temp_kf: -1.09 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01n" }], clouds: { all: 0 }, wind: { speed: 1.27, deg: 285.503 }, sys: { pod: "n" }, dt_txt: "2016-10-01 21:00:00" }, { dt: 1475366400, main: { temp: 293.49, temp_min: 293.49, temp_max: 294.306, pressure: 1009.32, sea_level: 1021.42, grnd_level: 1009.32, humidity: 57, temp_kf: -0.82 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01n" }], clouds: { all: 0 }, wind: { speed: 1.22, deg: 349.001 }, sys: { pod: "n" }, dt_txt: "2016-10-02 00:00:00" }, { dt: 1475377200, main: { temp: 293.13, temp_min: 293.13, temp_max: 293.672, pressure: 1011.03, sea_level: 1023.03, grnd_level: 1011.03, humidity: 61, temp_kf: -0.55 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01d" }], clouds: { all: 0 }, wind: { speed: 1.42, deg: 6.50092 }, sys: { pod: "d" }, dt_txt: "2016-10-02 03:00:00" }, { dt: 1475388000, main: { temp: 300.82, temp_min: 300.82, temp_max: 301.096, pressure: 1012.55, sea_level: 1024.46, grnd_level: 1012.55, humidity: 72, temp_kf: -0.27 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01d" }], clouds: { all: 0 }, wind: { speed: 2.06, deg: 10.5145 }, sys: { pod: "d" }, dt_txt: "2016-10-02 06:00:00" }, { dt: 1475398800, main: { temp: 304.139, temp_min: 304.139, temp_max: 304.139, pressure: 1011.34, sea_level: 1023.24, grnd_level: 1011.34, humidity: 70, temp_kf: 0 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01d" }], clouds: { all: 0 }, wind: { speed: 2.96, deg: 15.0044 }, sys: { pod: "d" }, dt_txt: "2016-10-02 09:00:00" }, { dt: 1475409600, main: { temp: 305.239, temp_min: 305.239, temp_max: 305.239, pressure: 1009.96, sea_level: 1021.78, grnd_level: 1009.96, humidity: 52, temp_kf: 0 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01d" }], clouds: { all: 0 }, wind: { speed: 4.06, deg: 21.503 }, sys: { pod: "d" }, dt_txt: "2016-10-02 12:00:00" }, { dt: 1475560800, main: { temp: 300.429, temp_min: 300.429, temp_max: 300.429, pressure: 1012.43, sea_level: 1024.37, grnd_level: 1012.43, humidity: 74, temp_kf: 0 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01d" }], clouds: { all: 0 }, wind: { speed: 2.1, deg: 292.503 }, sys: { pod: "d" }, dt_txt: "2016-10-04 06:00:00" }, { dt: 1475571600, main: { temp: 304.116, temp_min: 304.116, temp_max: 304.116, pressure: 1011.18, sea_level: 1023, grnd_level: 1011.18, humidity: 70, temp_kf: 0 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01d" }], clouds: { all: 0 }, wind: { speed: 2.21, deg: 316.5 }, sys: { pod: "d" }, dt_txt: "2016-10-04 09:00:00" }, { dt: 1475582400, main: { temp: 305.353, temp_min: 305.353, temp_max: 305.353, pressure: 1009.66, sea_level: 1021.39, grnd_level: 1009.66, humidity: 49, temp_kf: 0 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01d" }], clouds: { all: 0 }, wind: { speed: 2.39, deg: 346.503 }, sys: { pod: "d" }, dt_txt: "2016-10-04 12:00:00" }, { dt: 1475593200, main: { temp: 300.609, temp_min: 300.609, temp_max: 300.609, pressure: 1009.85, sea_level: 1021.74, grnd_level: 1009.85, humidity: 45, temp_kf: 0 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01n" }], clouds: { all: 0 }, wind: { speed: 1.68, deg: 16.0005 }, sys: { pod: "n" }, dt_txt: "2016-10-04 15:00:00" }, { dt: 1475604000, main: { temp: 296.264, temp_min: 296.264, temp_max: 296.264, pressure: 1010.76, sea_level: 1022.71, grnd_level: 1010.76, humidity: 51, temp_kf: 0 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01n" }], clouds: { all: 0 }, wind: { speed: 1.48, deg: 85.0018 }, sys: { pod: "n" }, dt_txt: "2016-10-04 18:00:00" }, { dt: 1475614800, main: { temp: 294.28, temp_min: 294.28, temp_max: 294.28, pressure: 1010.2, sea_level: 1022.3, grnd_level: 1010.2, humidity: 56, temp_kf: 0 }, weather: [{ id: 800, main: "Clear", description: "clear sky", icon: "01n" }], clouds: { all: 0 }, wind: { speed: 1.27, deg: 123.501 }, sys: { pod: "n" }, dt_txt: "2016-10-04 21:00:00" }],
    result = [];        

data.forEach(function (a) {
    var date = a.dt_txt.slice(0, 10);
    if (!this[date]) {
        this[date] = { date: date, max: -Infinity };
        result.push(this[date]);
    }
    this[date].max = Math.max(this[date].max , a.main.temp);
}, Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }