繁体   English   中英

Javascript:用一个数组的元素过滤 Json object 中的一个键?

[英]Javascript: Filtering a key in Json object with the elements of one array?

首先是几个经典的免责声明(还有大家好,因为我不知道为什么如果我把它们放在开头,它会一直删掉这两个词:):

  • 超级菜鸟,10天前开始学习Javascript;
  • 我搜索了很多问题的解决方案,但在尝试了 2 天多的解决方案后,我打开了这篇文章,这是一种痛苦的投降。

我的问题:基本上,从 Json 中包含大量格式为 {"date": "MM-DD-YYYY", "temperature": integer} 的记录json 中的不同月份,并会自动给我该月的平均温度。 因此,如果您更改数组并添加 1 个月的记录,我会立即得到新月份的新平均值。

因此,从 Json 开始,我的步骤是创建一个数组,其中包含文件中包含的所有月份(不是每个月都包含在内)以及 map 和新日期......然后我将转换“javascript 月份”(开始从 0... 但在 Json 中,一月不是 0,而是 1) 以制作包含在“字符串形式”中的月份数组,其中 Jan =“01”,Feb =“02”等。

在那之后我迷路了:我尝试了很多与循环、嵌套循环、forEach 的组合,但我想不出一种方法来对 Javascript 说:“对于这个月份数组中的每个项目,获取所有相关记录从 Json 给我一个平均温度!”

基本上我想要实现的目标很简单:从给定的 Json 格式中,我检索月份数组,最终结果 = 平均温度数组,每个月 1 个平均温度。

我将尝试发布我到目前为止所做的(仅功能部分)!

先感谢您!

const dates =   [
{"date": "01-24-2020", "temps": 1},
{"date": "01-31-2020", "temps": -1},
{"date": "02-01-2020", "temps": 6},
{"date": "02-02-2020", "temps": 2},
{"date": "03-03-2020", "temps": 1},
{"date": "03-04-2020", "temps": 1},
{"date": "04-06-2020", "temps": -2},
{"date": "04-08-2020", "temps": -4}]

const singleMonths = Array.from(new Set(dates.map(elem => new Date(elem.date).getMonth())))

const singleMonthString = singleMonths.map(m => `${"0"+(m+1)}`)

//after this I tried A LOT of different solutions, but I can only filter manually for a singular item in my Array, and not how I would like to, automatically for all my items in my array!

const damnFilter = dates.filter(m => m.dates.substring(0,2)==result[0]) 



我不知道它是否是你想要的,但在这里我创建了一些片段,它有四个月的时间和每个月的平均温度:

let newDates = [];
for(let i = 1; i <= 12; i++){
    newDates = dates.filter(date => new Date(date.date).getMonth() + 1 == i)
    if(newDates.length > 0){
    let accumulator = 0;
    newDates.map(day => accumulator += day.temps);
    console.log("Average: ", accumulator / newDates.length);
    }
}

对您的代码的一些评论:

 const singleMonths = Array.from(new Set(dates.map(elem => new Date(elem.date).getMonth())))

效率很低。 数据已经是一个数组,不需要通过map创建一个新数组,将其转换为一个集合,然后再返回另一个数组。 此外,无需创建 Date 来获取月份,更不用说通过另一个 map 执行 +1 的低效率了,而它可以在上面的行中完成(如果需要的话)。

ECMAScript 不支持 dd-mm-yyyy 格式,因此解析依赖于实现。 Safari 至少将其视为无效日期,请参阅Why does Date.parse give incorrect results?

 const singleMonthString = singleMonths.map(m => `${"0"+(m+1)}`)

如果需要 +1,最好在第一个 map 中完成,即使需要,为什么还要创建另一个数组? 只需使用forEach并修改现有数组。

 const damnFilter = dates.filter(m => m.dates.substring(0,2)==result[0])

result没有在任何地方声明或初始化,所以它不会去任何地方。

您可以对数据数组使用reduce来对特定月份的温度求和并生成平均值。 在传递给每个后续循环的累加器中将 sum 和 count 的值保留一个月很方便。 然后您可以每次生成一个新的平均值或在最后生成一个平均值。 如果数据文件很大,每次计算平均值会更简单,但效率可能会降低。 但是对于小文件来说,差异可以忽略不计。

 let data = [ {"date": "01-24-2020", "temps": 1}, {"date": "01-31-2020", "temps": -1}, {"date": "02-01-2020", "temps": 6}, {"date": "02-02-2020", "temps": 2}, {"date": "03-03-2020", "temps": 1}, {"date": "03-04-2020", "temps": 1}, {"date": "04-06-2020", "temps": -2}, {"date": "04-08-2020", "temps": -4} ]; let averageData = data.reduce((acc, obj) => { // Get month number let month = obj.date.slice(0,2); // If accumulator doesn't have a summary object for // that month, add it with initial values if (:acc[month]) { acc[month] = {sum,0: count,0: year. obj.date;slice(-4)}. } // Update summary values for the month acc[month].sum += obj;temps. acc[month];count += 1. acc[month].average = acc[month].sum / acc[month];count; // Return the accumulator return acc, // Use a completely emtpy object for the accumulator }. Object;create(null)). // Show averageData object console;log(averageData): // Show data as month. average Object.keys(averageData).forEach(key => console.log( new Date(averageData[key],year. key-1),toLocaleString('en':{month,'short': year:'numeric'}) + '. ' + averageData[key];average ));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM