簡體   English   中英

按日期對數組進行分組,並計算平均值

[英]Group array by date, and calculate average

我有下面的數組,我試圖按天分組,然后計算平均血液讀數。

[ { user: '59fcced3c40317c657878b5c',
    timestamp: 1509715440,
    blood: 8.2,
    id: '59fc6e42c6bc1c0223757c8e' },
  { user: '59fcced3c40317c657878b5c',
    timestamp: 1509698760,
    blood: 13.7,
    id: '59fc5c755109756616d29b49' },
  { user: '59fcced3c40317c657878b5c',
    timestamp: 1509694440,
    blood: 7.2,
    id: '59fc5ba65109756616d29b48' },
  { user: '59fcced3c40317c657878b5c',
    timestamp: 1509692580,
    blood: 3.4,
    id: '59fc5b915109756616d29b47' },
  { user: '59fcced3c40317c657878b5c',
    timestamp: 1509665040,
    blood: 8.7,
    id: '59fc4cf98e66f1e0065f4ff6' }]

我已經設法按天對它進行分組,但是我一生無法弄清楚如何進行進一步的分組:

 let object = _.groupBy(results, (result) => moment.unix(result['timestamp']).startOf('day'));

我在jsfiddle中有以下代碼: https ://jsfiddle.net/0yn900jb。

您可以使用Array#reduce以日期字符串(刪除時間之后)作為鍵將值收集到Map 然后Array#map將結果映射回對象,並計算平均值。

注意:我將時間戳更改為顯示2天。

 const data = [{"user":"59fcced3c40317c657878b5c","timestamp":1509715440,"blood":8.2,"id":"59fc6e42c6bc1c0223757c8e"},{"user":"59fcced3c40317c657878b5c","timestamp":1509698760,"blood":13.7,"id":"59fc5c755109756616d29b49"},{"user":"59fcced3c40317c657878b5c","timestamp":1509694440,"blood":7.2,"id":"59fc5ba65109756616d29b48"},{"user":"59fcced3c40317c657878b5c","timestamp":1509573320,"blood":3.4,"id":"59fc5b915109756616d29b47"},{"user":"59fcced3c40317c657878b5c","timestamp":1509573320,"blood":8.7,"id":"59fc4cf98e66f1e0065f4ff6"}]; const averages = [...data.reduce((m, o) => { const date = new Date(o.timestamp * 1000); // create a date from time stamp const day = new Date(date.getFullYear(), date.getMonth(), date.getDate()); // create the day date const item = m.get(day.toLocaleDateString()) || { day, blood: [] }; // get the item from the map by the date string, or create a new one item.blood.push(o.blood); // add the blood number return m.set(day.toLocaleDateString(), item); // store the item by the time string }, new Map).values()] // spread the map to an array of values .map(({ day, blood }) => ({ // map the array to and object day, blood: blood.reduce((a, b) => a + b) / blood.length // calculate bloody average })); console.log(averages); 

您可以按以下方式擴展lodash解決方案:

 let results = [ { user: '59fcced3c40317c657878b5c', timestamp: 1509715440, blood: 8.2, id: '59fc6e42c6bc1c0223757c8e' }, { user: '59fcced3c40317c657878b5c', timestamp: 1509698760, blood: 13.7, id: '59fc5c755109756616d29b49' }, { user: '59fcced3c40317c657878b5c', timestamp: 1509694440, blood: 7.2, id: '59fc5ba65109756616d29b48' }, { user: '59fcced3c40317c657878b5c', timestamp: 1509692580, blood: 3.4, id: '59fc5b915109756616d29b47' }, { user: '59fcced3c40317c657878b5c', timestamp: 1509665040, blood: 8.7, id: '59fc4cf98e66f1e0065f4ff6' }]; let object = _.chain(results) .groupBy((result) => moment.unix(result['timestamp']).startOf('day')) .map((entries, day) => [day, _.meanBy(entries, entry => entry.blood)]) .fromPairs() .value(); console.log(object); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM