读取数据D3时删除行 [英] Removing rows when reading data D3
本文介绍了读取数据D3时删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个示例文件sample.csv:
Say I have a sample file sample.csv:
row,col,value
1,1,2
1,2,3
1,3,NA
在d3中读取数据时,您会执行以下操作:
When reading data in d3 you do something like:
d3.csv("sample.csv", function(data) {
data.forEach(function(d) {
d.value = +d.value;
});
但是,对于NA值+ d.value将返回NaN.如何从数据中排除NaN值.即读取数据,并且只接受具有数字值的行
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
谢谢!
推荐答案
在尝试添加数据之前,可以对数据调用isNaN
:
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;
});
});
这假设您的数字都是以10为底的整数.
This assumes your numbers will all be base-10 integers.
这篇关于读取数据D3时删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文