使用数组过滤器从csv过滤数据无法正常工作

时间:2018-09-25 16:40:27

标签: d3.js array-filter

我的数据以csv格式如下。

Id,Title,Year,Runtime,Country,imdbRating,imdbVotes,预算,总价,WinsNoms,IsGoodRating 13,《黑暗中的孤独》,2005年,96年,“加拿大,德国,美国”,2.3,37613,20000000,8178569,9,0 38,Boogeyman,2005,89,“美国,新西兰,德国”,4.1,25931,20000000,67192859,0,0 52,康斯坦丁,2005,121,“美国,德国”,6.9,236091,75000000,221594911,11,1 62,《疯女人日记》,2005,116,美国,5.6,10462,5500000,50458356,26,0 83,Fever Pitch,2005,104,“美国,德国”,6.2,36198,40000000,50071069,9,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的新手,所以我可能犯了一个基本错误。

1 个答案:

答案 0 :(得分:0)

与下面的操作相同,但没有达到预期的效果。

  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); });