2016-05-06 217 views
1

我想按年篩選我的數據,但不知道在哪裏/如何從我的數據集(從csv加載)解析出我的年份。 (draw(d)和plot_loans(d)函數都被調用)。 我得到的錯誤:TypeError:d.ListingCreationDate.getUTCFullYear不是一個構造函數。TypeError:d.ListingCreationDate.getUTCFullYear不是構造函數

function draw(d) { 
function plot_loans(d){ 
function update(year){ 
       var filtered = d.filter(function(d){ 
        return new d.ListingCreationDate.getUTCFullYear() === year; 
        }); 
} 
} 

這是我的裝載功能:

d3.csv("loandata_sample.csv", function(d) { 
    return { 
    ListingKey: d.ListingKey, 
    ListingCreationDate: Date(d.ListingCreationDate) 
    }; 

回答

1

你的代碼,這部分給出這樣的錯誤:

return new d.ListingCreationDate.getUTCFullYear() === year;

new關鍵字試圖使一個新的對象從一個構造函數,你沒有提供。根據您想如何存儲日期,您應該將其更改爲:

return (new Date(d.ListingCreationDate)).getUTCFullYear() === year;

(如果d.ListingCreationDate)是一個字符串

或: return d.ListingCreationDate.getUTCFullYear() === year;

(如d .ListingCreationDate)是一個Date對象。在這種情況下,你必須初始化日期對象,改變ListingCreationDate: Date(d.ListingCreationDate)ListingCreationDate: new Date(d.ListingCreationDate)

+0

當我試圖ListingCreationDate:新的日期(d.ListingCreationDate)我得到一個無效的日期返回 – mleafer

+0

當我實現你的第一個建議,(返程(新日期( d.ListingCreationDate))。getUTCFullYear()=== year;)和called update(2010);在控制檯窗口中,我收到一個錯誤,指出更新未定義。 – mleafer