2017-11-05 5 views
0

私は以下の配列を持っています。私は一日ごとにグループ化しようとしています。配列を日付でグループ化し、平均を計算します

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

私はjsfiddleでコードを持っている:私は日によってグループそれに管理してきましたが、私は私の人生のために任意の更を取得する方法を見つけ出すことはできません

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

ここに:https://jsfiddle.net/0yn900jb

答えて

1

次のようにあなたの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>

1

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

+0

感謝を。原則として動作するようですが、タイムスタンプは間違っています。彼らは現在、11月3日よりむしろ1970年1月18日に戻ってきています。 – K20GH

+1

@ K20GH - 修正されました。私の日付の操作は少し錆びています。 –

関連する問題