2017-05-27 82 views
0

這是對以前提出的問題(In javascript, how do I extract the month from date string "yyyy-mm-dd" or timeStamp?)的修改,很好地由@mhodges解決。在javascript中,如何從json數據創建嵌套數組或對象?

由於修改的性質,我認爲這是適當的,只是創建一個新的問題。

我有一個數據庫作爲json對象,每個都有它自己的JS毫秒時間戳,還有一個格式爲「yyyy-mm-dd」的日期字符串,以及一個分鐘條目。

我想使用時間戳作爲輸入算法,該算法將計算在給定的月份和年份中輸入了多少個日誌。

我還想算算總和的累計分鐘數記錄在一個給定的月份和年份。

例如,

log1: { 
    Date:"2017-05-24", 
    TimeStamp:1495612800, 
    Minutes: 15}, 
log2: { 
    Date:"2017-05-19", 
    TimeStamp:1495180800, 
    Minutes: 45}, 
log3: { 
    Date:"2017-04-24", 
    TimeStamp:1493020800, 
    Minutes:30}, 
log4: { 
    Date:"2016-08-15", 
    TimeStamp:1471248000, 
    Minutes:75} 

在這個例子中,算法將計算有兩個日誌2017年5月的一個月,日誌2017年4月的一個月和一個日誌在8月2016年

算法將在2017年4月和75分鐘月份在八月2016年

與修改的解決方案,任何幫助,也可以返回60分鐘的總和中的五月2017 30分鐘@先前提供的mhodges(請參閱(In javascript, how do I extract the month from date string "yyyy-mm-dd" or timeStamp?)),進入適當的嵌套輸出將不勝感激!

我無法修改@mhodges的解決方案來實現它,但是想象這樣的事情:

{ 
    "2016":{ 
    "8": { 
     occurrences: 1, 
     minutes: 75 
    } 
    }, 
    "2017":{ 
    "4": { 
     occurrences: 1, 
     minutes: 30 
    }, 
    "5": { 
     occurrences: 2, 
     minutes: 60 
    } 
    } 
} 

感謝

回答

1

從你期望的輸出來看,你可能想使用的日期值,而不是時間戳。您可以循環查看日誌並累計每個月的分鐘數。

這將是這個樣子:

var logs = [{ 
 
    Date: "2017-05-24", 
 
    TimeStamp: 1495612800, 
 
    Minutes: 15 
 
    }, 
 
    { 
 
    Date: "2017-05-19", 
 
    TimeStamp: 1495180800, 
 
    Minutes: 45 
 
    }, 
 
    { 
 
    Date: "2017-04-24", 
 
    TimeStamp: 1493020800, 
 
    Minutes: 30 
 
    }, 
 
    { 
 
    Date:"2016-08-15", 
 
    TimeStamp:1471248000, 
 
    Minutes:75 
 
    } 
 
]; 
 

 
var result = {}; 
 
for (var i = 0; i < logs.length; i++) { 
 
    var year = logs[i].Date.slice(0, 4); 
 
    var month = logs[i].Date.slice(5, 7); 
 
    if (!result[year]) { 
 
    result[year] = {}; 
 
    } 
 
    if (!result[year][month]) { 
 
    result[year][month] = { 
 
     occurences: 1, 
 
     minutes: logs[i].Minutes 
 
    } 
 
    continue; 
 
    } 
 
    result[year][month].occurences++; 
 
    result[year][month].minutes += logs[i].Minutes; 
 
} 
 

 
console.log(result);

+0

非常感謝您的解決方案。我之所以選擇它是因爲它對我來說似乎是最簡單的解決方案,能夠實現我試圖實現的目標。儘管如此,我仍然繼續使用timeStamp並對其稍加修改。 – esd100

0

下面是它的裂縫。

let i = arr.length; 
let obj = {}; 
while(i--){ 
    let [year, month, day] = arr[i].Date.split('-'); 

    //probably a better way of initializing a hashtree. 
    if(!obj[year]){ obj[year] = {}; } 
    if(!obj[year][month]){ obj[year][month] = {}} 

    let occur = obj[year][month]['occurences'] 
    let minutes = obj[year][month]['minutes'] 

    obj[year][month]['occurences'] = (occur === undefined) ? 1 : occur + 1; 
    obj[year][month]['minutes'] = (minutes === undefined) ? arr[i].Minutes : arr[i].Minutes + minutes; 
} 

https://jsfiddle.net/25n4jo4e/

+0

感謝您的解決方案。它絕對是優雅的,我仍然可以修改我的代碼來使用它,因爲我不必改變太多。我不確定堆棧溢出是如何工作的,但如果有幫助,我很高興給你一個大拇指或複選標記。 – esd100

1

似乎是一個好問題與Array.prototype.mapArray.prototype.reduce以及一些內置的日期方法解決。

const logs = [ 
 
    { 
 
    Date:"2017-05-24", 
 
    TimeStamp:1495612800, 
 
    Minutes: 15 
 
    }, 
 
    { 
 
    Date:"2017-05-19", 
 
    TimeStamp:1495180800, 
 
    Minutes: 45 
 
    }, 
 
    { 
 
    Date:"2017-04-24", 
 
    TimeStamp:1493020800, 
 
    Minutes:30 
 
    }, 
 
    { 
 
    Date:"2016-08-15", 
 
    TimeStamp:1471248000, 
 
    Minutes:75 
 
    }] 
 

 
// mapping over the logs, transforming each so that we have data in the right form (ie. years, months, and minutes; occurences we'll get later). 
 
const flat = logs.map(log => { 
 
    const date = new Date(log.Date) 
 
    return { 
 
    year: date.getFullYear(), 
 
    month: date.getMonth() + 1, 
 
    minutes: log.Minutes, 
 
    } 
 
}) 
 

 
const hierarchy = flat.reduce((hier, log) => { 
 
    // As we're building the result, we need to init things if they don't already exist 
 
    hier[log.year] = hier[log.year] || {} 
 
    // The lowest level will have this form 
 
    hier[log.year][log.month] = hier[log.year][log.month] || 
 
    { 
 
     occurences: 0, 
 
     minutes: 0 
 
    } 
 
    
 
    // Accumulate minutes and occurences as we go 
 
    hier[log.year][log.month].minutes += log.minutes 
 
    hier[log.year][log.month].occurences += 1 
 

 
    return hier 
 
}, {}) 
 

 
console.log(hierarchy)

+0

謝謝你的解決方案。我認爲它肯定會奏效。我不確定堆棧溢出是如何工作的,但如果有幫助,我很高興給你一個大拇指或複選標記。 – esd100

相關問題