我的数据以csv格式显示如下。
Id,Title,Year,Runtime,Country,imdbRating,imdbVotes,Budget,Gross,WinsNoms,IsGoodRating 13,Alone in the Dark,2005,96,“加拿大,德国,美国”,2.3,37613,20000000,8178569,038,Boogeyman,2005,89,“美国,新西兰,德国”,4.1,25931,20000000,67192859,0,052,Constantine,2005,121,“美国,德国”,6.9,236091,750000,221594911,11,62,疯女日记,2005,116,USA,5.6,102,5500,000,50458356,26,083,狂热,2005,104,“美国,德国”,6.2,36198,400,00000,500769,1,1
我正在尝试过滤数据,如下所示,但所有的过滤都不起作用。
d3.csv("movies.csv", function(error, data) {
// change string (from CSV) into number format
data.forEach(function(d) {
d.imdbRating = +d.imdbRating;
d["WinsNoms"] = +d["WinsNoms"];
d["IsGoodRating"] = +d["IsGoodRating"]
});
var rating0 = data.filter(function(d){ return d["IsGoodRating"] = 0});
rating0.forEach(function(d) { console.log(d); });
//the above line does not give me anything on the console
var rating1 = data.filter(function(d){ return d["IsGoodRating"] = 1});
rating1.forEach(function(d) { console.log(d); });
//the above line gives me an output of all the records with both IsGoodRating which are 0 and 1 but the output shows as 1 which is not what the data has.任何帮助都将不胜感激。我是d3.js的新手,所以我可能犯了一个基本的错误。
发布于 2018-09-26 00:59:09
执行与下面相同的操作可以达到预期效果,但数组筛选器不会。
var rating0 = data.filter(function(d)
{
if (d["IsGoodRating"] == 0)
{
return d;
}
})
// var rating0 = data.filter(function(d){ return d.IsGoodRating = 0}); This array filter is not working for some reason
rating0.forEach(function(d) { console.log(d.IsGoodRating); });
var rating1 = data.filter(function(d)
{
if (d["IsGoodRating"] == 1)
{
return d;
}
})
// var rating1 = data.filter(function(d){ return d["IsGoodRating"] != 0});This array filter is not working for some reason
rating1.forEach(function(d) { console.log(d.IsGoodRating); });https://stackoverflow.com/questions/52503032
复制相似问题