【问题标题】:TypeError: data.slice is not a function Loading csv d3 issueTypeError:data.slice 不是函数加载 csv d3 问题
【发布时间】:2017-03-25 22:27:42
【问题描述】:
var sales = d3.csv("Sales Export Friendly 3-19-17.csv", function(error, data) {
    //var parseDate = d3.time.format("%m/%d/%Y %H:%M:%S %p").parse;
    return {
        unit: data["Unit Booked"],
        date: new Date(data["Booking Date"]).getMonth() + 1,
        checkin: new Date(data["Checkin"]).getMonth() + 1,
        LOS: new Date(data["Checkout"]).valueOf() - new Date(data["Checkin"]).valueOf()/(24*60*60*1000),
        total: +data["Total Stay"],
        avgNight: (+data["Total Stay"]) / ((new Date(data["Checkout"]).valueOf() - new Date(data["Checkin"]).valueOf())/(24*60*60*1000))
        }
});

console.log(sales);
console.log(d3.keys(sales[0]));


parcoords = d3.parcoords()("#TopLeft");

parcoords

关于销售退货的控制台记录语句

Object { header: Cn/u.header(), mimeType: Cn/u.mimeType(), responseType: Cn/u.responseType(), response: Cn/u.response(), get: Cn/</u[n](), post: Cn/</u[n](), send: Cn/u.send(), abort: Cn/u.abort(), on: M/<(), row: e/o.row() }

我不确定这个奇怪的物体是怎么发生的。

并且下面的 console.log 语句返回一个空数组。

最后,我得到一个 TypeError: data.slice is not a function when calling parcoords

【问题讨论】:

    标签: javascript csv d3.js


    【解决方案1】:

    直到你的ante-penultimate question,你做对了:

    d3.csv(url, function(data){
        //code here
    });
    

    然后,我不知道为什么,在你的previous question(现在我才注意到它)中,在这个问题中,你开始这样做了:

    var data = d3.csv(url, function(data){
        //the rest of the code
    

    这行不通。

    这是问题所在:d3.csv 不返回任何东西!从技术上讲,它返回一个与请求相关的对象(运行sn -p查看)...

    var test = d3.csv("https://gist.githubusercontent.com/mbostock/3887051/raw/805adad40306cedf1a513c252ddd95e7c981885a/data.csv", function(data){
    });
    
    console.log(test);
    &lt;script src="https://d3js.org/d3.v4.min.js"&gt;&lt;/script&gt;

    ...这不是你想要的。

    因此,回到你正在做的事情......

    d3.csv("Sales Export Friendly 3-19-17.csv", function(error, data) {
        //code here
    })
    

    ...删除var sales

    另外,请记住d3.csv异步。所以,你必须在回调中console.log你的变量:

    d3.csv("Sales Export Friendly 3-19-17.csv", function(error, data) {
        console.log(data)//this works 
    });
    
    console.log(data)//this will not work
    

    【讨论】:

    • 嘿,再次感谢您,但我现在收到一条警告,说我的代码在我的 return 语句后无法访问。我该如何解决这个问题?
    • 您必须在 Plunker/CodePen/whatever 中发布代码的最低工作版本...
    • 希望我的格式正确...我确实在控制台中看到返回错误后无法访问的代码,所以应该没问题:jsfiddle.net/jw7wc650/3
    • 嗯,我把我的回报放在地图功能地图中,这似乎解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 2013-02-05
    • 2016-07-30
    • 2017-03-31
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    相关资源
    最近更新 更多