【问题标题】:Use Weather API and sort it by date使用 Wea​​ther API 并按日期排序
【发布时间】:2019-09-21 23:39:16
【问题描述】:

我正在尝试按日期过滤数组,然后为每一天制作一个组件。对我来说最大的障碍是日期以 UTC 显示,我不确定如何将其转换为可用的格式来循环播放。除非有人提出更有效的方法,否则目标是能够对日期数据进行分组,调​​用该数组(即 firstDay[0] )并循环遍历这些数据以找到当天的最高温度、平均湿度等。但是我认为第一步是按日期排序。即五天[0].maxtemp

JSON https://api.openweathermap.org/data/2.5/forecast?q=Austin,USA&appid=583f803dfc6a7f2f96ff9957c330c2b0&units=imperial

            var firstDay = [];
            var firstDayVar;
            for (i = 0; i < items.list.length; i++){
                let item = items.list[i].dt
                let date = new Date(item*1000)
                let dateNumber = date.getDate()
                let date_text = items.list[i].dt_txt
                // Thought this would be useful to organize by but not really applicable?
                let dateString = date_text.substring(0,10)


                console.log(dateString)
                //outputs yyyy-mm-dd
                if (firstDayVar === undefined){
                    firstDayVar = dateNumber
                } else if (firstDayVar !== dateNumber) {
                    firstDayVar = dateNumber
                    firstDay.push(firstDayVar);
            }
        }

【问题讨论】:

  • 如何按日期对它们进行分组,它们是 40 个单独的唯一日期?
  • 您需要展示什么以及如何展示?
  • 每 3 小时预测 5 天。因此,如果您在第一天中午检查可能有 4 个阵列,那么第二天(以及接下来的 4 个)有 8 个阵列。我最终想显示每天的最高温度、湿度等,但无法通过制作每个特定日期的数组。
  • 数据似乎已按日期/时间、“dt”时间戳或“dt_text”属性按升序排序。我也没有看到指定的时区。
  • 它以 UTC 显示,这就是 date 和 dateNumber 变量将其转换为可读日期的地方。但是不知道如何从那里过滤信息。

标签: javascript reactjs api


【解决方案1】:

这就是我根据日期对您的数据进行分组的方式。现在您可以根据日期循环它们,每个日期都有一个来自您的 api 列表属性的原始对象数组。

该 api 为您提供 [DATE]_[TIME] 的数组项,我们的目标是将相同的日期分组到 [DATE] 的字典中,其中每个单独的日期将包含原始项目的列表(针对该特定日期)

[ D1.T1, D1.T2, D2.T1, D3.T1, D3.T2, D3.T3 ] // 原始项目数组

.

{ D1: [D1.T1, D1.T2], D2: [D2.T1], D3: [D3.T1, D3.T2, D3.T3] } // 对象按日期分组项目

如果您仔细观察,我们的想法是确保您的数据安全,从而确保您的原始结构安全。现在,您循环分组数据的键,您可以访问单个日期的所有项目。

fetch('https://api.openweathermap.org/data/2.5/forecast?q=Austin,USA&appid=583f803dfc6a7f2f96ff9957c330c2b0&units=imperial').then(response => response.json()).then(data => {
  const groupedData = data.list.reduce((days, row) => {
    const date = row.dt_txt.split(' ')[0];
    days[date] = [...(days[date] ? days[date]: []), row];
    return days;
  }, {});
  
  for(let date of Object.keys(groupedData)){
    console.log('Date:', date); 
    // current date -> date
    // original items array for this date -> groupedData[date]
    console.log('RowCount:', groupedData[date].length);
    console.log('MaxTemp:', getMax(groupedData[date], 'temp_max'));
    console.log('MinTemp:', getMin(groupedData[date], 'temp_min'));
    console.log('MaxHumidity:', getMax(groupedData[date], 'humidity'));
    
    console.log('\n\n');
  }
});

function getMax(arr, attr){
  return Math.max.apply(Math, arr.map(item => item.main[attr]));
}

function getMin(arr, attr){
  return Math.min.apply(Math, arr.map(item => item.main[attr]));
}

由于您的大部分数据都在列表内的主要属性中,因此我创建了 2 个辅助函数,它们将比较所有项目并获取传递属性的最大值和最小值,例如:temp_max、temp_min、湿度等。您可以获得自己的自定义喜欢的方法。

希望这会有所帮助!

【讨论】:

  • 哇,谢谢乔伊!我要花点时间来解决这个问题,但它正在工作,所以这是一个开始。谢谢!
  • 有什么需要解释的吗?我试着把每一个想法都告诉我是否还有更多
【解决方案2】:

要获取仅包含具有特定日期字符串的对象(例如“2019-09-23”)的数组,我将使用 Array.prototype.filter 方法和 String.prototype.includes 方法来仅返回具有 dt_text 的对象包括日期字符串:

(where list is the array returned from the API)

var firstDayList = list.filter(item => item.dt_txt.includes("2019-09-23"))

【讨论】:

  • 我喜欢它的清洁度,但遗憾的是过滤的日子每天都会改变
  • 感谢@MPortman!每当您调用 API 时,只需创建一个 dateStrings 数组,然后(其中 list 是从 API 返回的数组,dateStringList 是 dateStrings 数组) var firstDayList = list.filter(item -> item.dt_text.includes(dateStringList [0])
【解决方案3】:

我不确定我是否得到了你,但从外观上看,天气 API 中的 data 已经按日期按升序排列,这要归功于 dt: key all you need to do 是从列表中渲染你的组件

list.map(day => <MyAwesomeComponent 
                   maxTemp={day.main.temp_max} 
                   humidityAvg={day.main.humidity}  />)

【讨论】:

    【解决方案4】:

    我认为通过函数式方法,您可以从阅读的简单性中受益,并且在您的需求发生变化时,可以轻松确定在代码中放置更改的位置。

    在下面的sn-p中,我尝试了:

    • 获取数组数据
    • 按年分组
    • 获取每天的最高温度

    当然,您可以选择输出数据结构。如果您阅读该组合,则只需更改最后一个返回数据的函数即可更改它。

    我创建了一些函数来帮助我完成这个过程,尽管你可以导入一些函数库,比如 Ramda,而不必自己编写所有这些函数。希望对您有所帮助!

    const data = {"cod":"200","message":0.0182,"cnt":40,"list":[{"dt":1569110400,"main":{"temp":82.89,"temp_min":82.88,"temp_max":82.89,"pressure":1013.51,"sea_level":1013.51,"grnd_level":980.71,"humidity":70,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"clouds":{"all":17},"wind":{"speed":9.04,"deg":156.373},"rain":{"3h":0.937},"sys":{"pod":"n"},"dt_txt":"2019-09-22 00:00:00"},{"dt":1569121200,"main":{"temp":77.63,"temp_min":77.63,"temp_max":77.63,"pressure":1015.99,"sea_level":1015.99,"grnd_level":983.15,"humidity":82,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":9.33,"deg":155.707},"sys":{"pod":"n"},"dt_txt":"2019-09-22 03:00:00"},{"dt":1569132000,"main":{"temp":75.42,"temp_min":75.41,"temp_max":75.42,"pressure":1015.25,"sea_level":1015.25,"grnd_level":982.17,"humidity":87,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":9.62,"deg":161.426},"sys":{"pod":"n"},"dt_txt":"2019-09-22 06:00:00"},{"dt":1569142800,"main":{"temp":73.9,"temp_min":73.9,"temp_max":73.91,"pressure":1015.59,"sea_level":1015.59,"grnd_level":982.35,"humidity":91,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":20},"wind":{"speed":8.12,"deg":168.281},"sys":{"pod":"n"},"dt_txt":"2019-09-22 09:00:00"},{"dt":1569153600,"main":{"temp":73.08,"temp_min":73.08,"temp_max":73.08,"pressure":1015.14,"sea_level":1015.14,"grnd_level":982.18,"humidity":93,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":45},"wind":{"speed":6.73,"deg":167.593},"sys":{"pod":"n"},"dt_txt":"2019-09-22 12:00:00"},{"dt":1569164400,"main":{"temp":79,"temp_min":79,"temp_max":79,"pressure":1016.86,"sea_level":1016.86,"grnd_level":983.86,"humidity":78,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"clouds":{"all":16},"wind":{"speed":9.69,"deg":178.215},"sys":{"pod":"d"},"dt_txt":"2019-09-22 15:00:00"},{"dt":1569175200,"main":{"temp":85.01,"temp_min":85.01,"temp_max":85.01,"pressure":1015.13,"sea_level":1015.13,"grnd_level":982.73,"humidity":62,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":42},"wind":{"speed":9.01,"deg":196.503},"sys":{"pod":"d"},"dt_txt":"2019-09-22 18:00:00"},{"dt":1569186000,"main":{"temp":87.89,"temp_min":87.89,"temp_max":87.89,"pressure":1012.88,"sea_level":1012.88,"grnd_level":980.86,"humidity":53,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":35},"wind":{"speed":8.32,"deg":195.654},"sys":{"pod":"d"},"dt_txt":"2019-09-22 21:00:00"},{"dt":1569196800,"main":{"temp":83.03,"temp_min":83.03,"temp_max":83.03,"pressure":1012.49,"sea_level":1012.49,"grnd_level":980.17,"humidity":63,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":22},"wind":{"speed":4.74,"deg":193.203},"sys":{"pod":"n"},"dt_txt":"2019-09-23 00:00:00"},{"dt":1569207600,"main":{"temp":78.32,"temp_min":78.32,"temp_max":78.32,"pressure":1014.7,"sea_level":1014.7,"grnd_level":981.95,"humidity":70,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.61,"deg":162.944},"sys":{"pod":"n"},"dt_txt":"2019-09-23 03:00:00"},{"dt":1569218400,"main":{"temp":76.21,"temp_min":76.21,"temp_max":76.21,"pressure":1015.25,"sea_level":1015.25,"grnd_level":982.44,"humidity":83,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":2},"wind":{"speed":8.75,"deg":165.85},"sys":{"pod":"n"},"dt_txt":"2019-09-23 06:00:00"},{"dt":1569229200,"main":{"temp":73.96,"temp_min":73.96,"temp_max":73.96,"pressure":1015.75,"sea_level":1015.75,"grnd_level":982.71,"humidity":89,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":5.75,"deg":180.824},"sys":{"pod":"n"},"dt_txt":"2019-09-23 09:00:00"},{"dt":1569240000,"main":{"temp":71.87,"temp_min":71.87,"temp_max":71.87,"pressure":1015.53,"sea_level":1015.53,"grnd_level":982.54,"humidity":97,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":3},"wind":{"speed":4.36,"deg":186.139},"sys":{"pod":"n"},"dt_txt":"2019-09-23 12:00:00"},{"dt":1569250800,"main":{"temp":79.65,"temp_min":79.65,"temp_max":79.65,"pressure":1017.18,"sea_level":1017.18,"grnd_level":984.35,"humidity":77,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":39},"wind":{"speed":7.11,"deg":193.795},"sys":{"pod":"d"},"dt_txt":"2019-09-23 15:00:00"},{"dt":1569261600,"main":{"temp":85.73,"temp_min":85.73,"temp_max":85.73,"pressure":1015.9,"sea_level":1015.9,"grnd_level":983.44,"humidity":57,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":57},"wind":{"speed":7.2,"deg":184.806},"sys":{"pod":"d"},"dt_txt":"2019-09-23 18:00:00"},{"dt":1569272400,"main":{"temp":88.16,"temp_min":88.16,"temp_max":88.16,"pressure":1013.63,"sea_level":1013.63,"grnd_level":981.49,"humidity":53,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":2},"wind":{"speed":8.66,"deg":184.39},"sys":{"pod":"d"},"dt_txt":"2019-09-23 21:00:00"},{"dt":1569283200,"main":{"temp":82.85,"temp_min":82.85,"temp_max":82.85,"pressure":1013,"sea_level":1013,"grnd_level":980.58,"humidity":65,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":1},"wind":{"speed":4.88,"deg":199.248},"sys":{"pod":"n"},"dt_txt":"2019-09-24 00:00:00"},{"dt":1569294000,"main":{"temp":78.71,"temp_min":78.71,"temp_max":78.71,"pressure":1014.56,"sea_level":1014.56,"grnd_level":981.68,"humidity":72,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":5.86,"deg":165.386},"sys":{"pod":"n"},"dt_txt":"2019-09-24 03:00:00"},{"dt":1569304800,"main":{"temp":76.09,"temp_min":76.09,"temp_max":76.09,"pressure":1014.45,"sea_level":1014.45,"grnd_level":981.57,"humidity":85,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":1},"wind":{"speed":9.28,"deg":176.517},"sys":{"pod":"n"},"dt_txt":"2019-09-24 06:00:00"},{"dt":1569315600,"main":{"temp":74.21,"temp_min":74.21,"temp_max":74.21,"pressure":1013.85,"sea_level":1013.85,"grnd_level":980.82,"humidity":88,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":7.74,"deg":179.718},"sys":{"pod":"n"},"dt_txt":"2019-09-24 09:00:00"},{"dt":1569326400,"main":{"temp":72.42,"temp_min":72.42,"temp_max":72.42,"pressure":1013.93,"sea_level":1013.93,"grnd_level":980.82,"humidity":95,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":0},"wind":{"speed":4.63,"deg":182.266},"sys":{"pod":"n"},"dt_txt":"2019-09-24 12:00:00"},{"dt":1569337200,"main":{"temp":79.25,"temp_min":79.25,"temp_max":79.25,"pressure":1014.69,"sea_level":1014.69,"grnd_level":981.9,"humidity":78,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":30},"wind":{"speed":8.63,"deg":183.908},"sys":{"pod":"d"},"dt_txt":"2019-09-24 15:00:00"},{"dt":1569348000,"main":{"temp":85.81,"temp_min":85.81,"temp_max":85.81,"pressure":1013.17,"sea_level":1013.17,"grnd_level":980.88,"humidity":60,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}],"clouds":{"all":53},"wind":{"speed":9.42,"deg":187.144},"rain":{"3h":0.125},"sys":{"pod":"d"},"dt_txt":"2019-09-24 18:00:00"},{"dt":1569358800,"main":{"temp":87.82,"temp_min":87.82,"temp_max":87.82,"pressure":1010.22,"sea_level":1010.22,"grnd_level":977.99,"humidity":53,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":42},"wind":{"speed":9.95,"deg":182.088},"sys":{"pod":"d"},"dt_txt":"2019-09-24 21:00:00"},{"dt":1569369600,"main":{"temp":83.21,"temp_min":83.21,"temp_max":83.21,"pressure":1009.42,"sea_level":1009.42,"grnd_level":977.01,"humidity":64,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":36},"wind":{"speed":7.67,"deg":177.945},"sys":{"pod":"n"},"dt_txt":"2019-09-25 00:00:00"},{"dt":1569380400,"main":{"temp":78.07,"temp_min":78.07,"temp_max":78.07,"pressure":1011.25,"sea_level":1011.25,"grnd_level":978.5,"humidity":73,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":2},"wind":{"speed":6.69,"deg":164.21},"sys":{"pod":"n"},"dt_txt":"2019-09-25 03:00:00"},{"dt":1569391200,"main":{"temp":74.71,"temp_min":74.71,"temp_max":74.71,"pressure":1010.9,"sea_level":1010.9,"grnd_level":977.86,"humidity":76,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":1},"wind":{"speed":8.99,"deg":168.103},"sys":{"pod":"n"},"dt_txt":"2019-09-25 06:00:00"},{"dt":1569402000,"main":{"temp":71.69,"temp_min":71.69,"temp_max":71.69,"pressure":1011.28,"sea_level":1011.28,"grnd_level":978.47,"humidity":84,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":37},"wind":{"speed":5.17,"deg":194.313},"sys":{"pod":"n"},"dt_txt":"2019-09-25 09:00:00"},{"dt":1569412800,"main":{"temp":70.28,"temp_min":70.28,"temp_max":70.28,"pressure":1011.42,"sea_level":1011.42,"grnd_level":978.5,"humidity":86,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":37},"wind":{"speed":5.46,"deg":186.551},"sys":{"pod":"n"},"dt_txt":"2019-09-25 12:00:00"},{"dt":1569423600,"main":{"temp":77.56,"temp_min":77.56,"temp_max":77.56,"pressure":1011.79,"sea_level":1011.79,"grnd_level":979,"humidity":71,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":8.12,"deg":193.902},"sys":{"pod":"d"},"dt_txt":"2019-09-25 15:00:00"},{"dt":1569434400,"main":{"temp":86.92,"temp_min":86.92,"temp_max":86.92,"pressure":1010.36,"sea_level":1010.36,"grnd_level":977.95,"humidity":50,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"clouds":{"all":0},"wind":{"speed":10.2,"deg":185.353},"sys":{"pod":"d"},"dt_txt":"2019-09-25 18:00:00"},{"dt":1569445200,"main":{"temp":88.89,"temp_min":88.89,"temp_max":88.89,"pressure":1007.85,"sea_level":1007.85,"grnd_level":975.65,"humidity":45,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":70},"wind":{"speed":9.8,"deg":182.987},"sys":{"pod":"d"},"dt_txt":"2019-09-25 21:00:00"},{"dt":1569456000,"main":{"temp":83.55,"temp_min":83.55,"temp_max":83.55,"pressure":1008.24,"sea_level":1008.24,"grnd_level":975.81,"humidity":57,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03n"}],"clouds":{"all":42},"wind":{"speed":5.12,"deg":185.16},"sys":{"pod":"n"},"dt_txt":"2019-09-26 00:00:00"},{"dt":1569466800,"main":{"temp":78.68,"temp_min":78.68,"temp_max":78.68,"pressure":1010.16,"sea_level":1010.16,"grnd_level":977.2,"humidity":60,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":9},"wind":{"speed":4.16,"deg":150.313},"sys":{"pod":"n"},"dt_txt":"2019-09-26 03:00:00"},{"dt":1569477600,"main":{"temp":74.57,"temp_min":74.57,"temp_max":74.57,"pressure":1010.71,"sea_level":1010.71,"grnd_level":977.75,"humidity":79,"temp_kf":0},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02n"}],"clouds":{"all":18},"wind":{"speed":10.51,"deg":175.199},"sys":{"pod":"n"},"dt_txt":"2019-09-26 06:00:00"},{"dt":1569488400,"main":{"temp":72.6,"temp_min":72.6,"temp_max":72.6,"pressure":1010.46,"sea_level":1010.46,"grnd_level":977.47,"humidity":85,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":9},"wind":{"speed":7.54,"deg":186.554},"sys":{"pod":"n"},"dt_txt":"2019-09-26 09:00:00"},{"dt":1569499200,"main":{"temp":70.79,"temp_min":70.79,"temp_max":70.79,"pressure":1010.83,"sea_level":1010.83,"grnd_level":977.8,"humidity":90,"temp_kf":0},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"clouds":{"all":5},"wind":{"speed":4.88,"deg":179.185},"sys":{"pod":"n"},"dt_txt":"2019-09-26 12:00:00"},{"dt":1569510000,"main":{"temp":78.81,"temp_min":78.81,"temp_max":78.81,"pressure":1011.75,"sea_level":1011.75,"grnd_level":978.79,"humidity":71,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":26},"wind":{"speed":6.93,"deg":183.796},"sys":{"pod":"d"},"dt_txt":"2019-09-26 15:00:00"},{"dt":1569520800,"main":{"temp":86.98,"temp_min":86.98,"temp_max":86.98,"pressure":1010.72,"sea_level":1010.72,"grnd_level":977.99,"humidity":49,"temp_kf":0},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}],"clouds":{"all":56},"wind":{"speed":8.81,"deg":158.347},"sys":{"pod":"d"},"dt_txt":"2019-09-26 18:00:00"},{"dt":1569531600,"main":{"temp":89.14,"temp_min":89.14,"temp_max":89.14,"pressure":1007.34,"sea_level":1007.34,"grnd_level":974.86,"humidity":42,"temp_kf":0},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"clouds":{"all":25},"wind":{"speed":10.07,"deg":167.638},"sys":{"pod":"d"},"dt_txt":"2019-09-26 21:00:00"}],"city":{"id":4671654,"name":"Austin","coord":{"lat":30.2711,"lon":-97.7437},"country":"US","population":790390,"timezone":-18000,"sunrise":1569068318,"sunset":1569112179}}
    
    /**
    * FUNCTIONS TO HELP IN THE COMPOSITION
    */
    const pluck = prop => obj => obj[prop];
    const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x);
    const map = f => arr => arr.map(f)
    const oMap = f => o => Object.assign({}, ...Object.keys(o).map(k => ({ [k]: f(o[k]) })));
    const groupBy = prop => arr => {
      return arr.reduce(function(groups, item) {
        const val = item[prop];
        groups[val] = groups[val] || [];
        groups[val].push(item);
        return groups;
      }, {});
    };
    
    /**
    * Function that takes an entry of the raw data array and adds a _day with the dd/mm/yyyy
    */
    const addDay = item => ({...item, _day: (new Date(item.dt*1000)).toLocaleDateString()});
    
    /**
    * Function that takes an array of entries and get the max temperature in it
    */
    const getMaxTempArr = arr => {
      const maxTemp = arr.reduce((acc, val) => {
        if (val.main.temp_max > acc) {
          acc = val.main.temp_max;
        }
        return acc;
      }, 0);
      return maxTemp;
    }
    
    const program = pipe(
      pluck('list'),
      map(addDay),
      groupBy('_day'),
      oMap(getMaxTempArr)
    )
    
    console.log(program(data))

    【讨论】:

      猜你喜欢
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多