Kashif Malak Kashif Malak - 2 months ago 5
Javascript Question

Filtering json array in javascript

I am getting following Json data from a weather service. I need a JavaScript function to filter out an array of highest main.temp for every dt_txt.

Example : I have 5 days data and every day has 8 records. I need to get one record with highest temperature for every day. Any help will be highly appreciated.

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

Answer

You could use a hash table for the dates and get the max values within the iteration.

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