2016-10-01 12 views
-4

天気予報サービスからJsonデータを取得しています。すべてのdt_txtのmain.tempの配列をフィルタリングするJavaScript関数が必要です。javascriptのjson配列をフィルタリングする

例:私は5日間のデータを持ち、毎日8レコードあります。私は毎日最高気温のレコードを1つ取得する必要があります。どんな助力も高く評価されます。

[{ "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" 
}] 
+0

?何を試しましたか? –

+1

私たちは本当にjsonダンプを必要としません。関連するプロパティと「...」を含めたり、残りの部分を除外するだけです。また、既にフィルタリングしようとしたコードを表示してください。 – Jecoms

答えて

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; }

+0

あなたの努力と迅速な対応のためにNinaさんにありがとうございました。 – kmalik

関連する問題