首先是幾個經典的免責宣告(大家好,因為我不知道為什么我把這兩個詞放在開頭!):
- 超級新手,10天前開始學習Javascript;
- 我為我的問題尋找了很多解決方案,但是在嘗試了 2 天多的解決方案之后,我打開了這篇文章,一種痛苦的投降。
我的問題:基本上,從包含大量記錄的 Json 格式為 {"date": "MM-DD-YYYY", "temperature": integer} 我正在嘗試構建一種互動式腳本,它將占用所有該json中的不同月份,并會自動給我該月的平均溫度。因此,如果您更改陣列并添加 1 個月的記錄,我會立即獲得新月份的新平均值。
因此,從 Json 開始,我的步驟是創建一個陣列,其中包含檔案中包含的所有月份(不是每個月都包含),其中包含地圖和新日期......然后我將轉換“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])
uj5u.com熱心網友回復:
對您的代碼的一些評論:
const singleMonths = Array.from(new Set(dates.map(elem => new Date(elem.date).getMonth())))
效率很低。資料已經是一個陣列,不需要通過map創建一個新陣列,將其轉換為一個集合,然后再回傳另一個陣列。此外,不需要創建 Date 來獲取月份,更不用說通過另一張地圖進行 1 的效率低下(如果需要的話)。
ECMAScript 不支持 dd-mm-yyyy 格式,因此決議依賴于實作。Safari 至少將其視為無效日期,請參閱為什么 Date.parse 給出不正確的結果?
const singleMonthString = singleMonths.map(m => `${"0" (m 1)}`)
如果需要 1,最好在第一張地圖中進行,即使您需要這樣做,為什么還要創建另一個陣列?只需使用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
));
uj5u.com熱心網友回復:
我不知道它是否是你想要的,但在這里我創建了一些片段,它有四個月和每個月的平均溫度:
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/434261.html
標籤:javascript 数组 json 日期 筛选
