Removing rows when reading data D3

∥☆過路亽.° 提交于 2020-04-10 04:10:11

问题


Say I have a sample file sample.csv:

row,col,value
1,1,2
1,2,3
1,3,NA

When reading data in d3 you do something like:

d3.csv("sample.csv", function(data) {
    data.forEach(function(d) {
    d.value = +d.value;
});

However, for the NA value +d.value will return NaN. How can I exclude NaN values from my data. i.e. read the data, and only take rows which have a number value

Thanks!


回答1:


You can call isNaN on the data before you try to add it:

d3.csv('sample.csv', function(data) {
    data = data.filter(function(d){
        if(isNaN(d.value)){
            return false;
        }
        d.value = parseInt(d.value, 10);
        return true;
    });
});

This assumes your numbers will all be base-10 integers.



来源:https://stackoverflow.com/questions/12922236/removing-rows-when-reading-data-d3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!