2017-11-05 3 views
0

Ich habe das folgende Array, und ich versuche, am Tag zu gruppieren, und dann berechnen Sie die durchschnittliche Blutwerte.Gruppieren Sie Array nach Datum, und berechnen Sie Durchschnitt

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

Ich habe zu einer Gruppe es von Tag geschafft, aber ich kann für das Leben von mir nicht vorstellen, wie weiter zu erhalten:

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

ich den Code in jsfiddle haben hier: https://jsfiddle.net/0yn900jb.

Antwort

1

Sie können Ihre lodash Lösung wie folgt erweitern:

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

Sie können Array#reduce verwenden, um Werte zu einer Map mit der Datumszeichenfolge (nach dem Entfernen der Zeit) als Schlüssel zu sammeln. Dann Array#map die Ergebnisse zurück zu Objekten, und berechnen Sie die Durchschnittswerte.

Hinweis: Ich habe die Zeitstempel geändert, um 2 Tage anzuzeigen.

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

Dank. Es scheint im Prinzip zu funktionieren, die Zeitstempel sind jedoch falsch. Sie kommen derzeit als Jan 18 1970 zurück, anstatt am 3. November usw. – K20GH

+1

@ K20GH - Fixed. Meine Datumsmanipulation ist ein bisschen rostig. –

Verwandte Themen