2016-05-06 164 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