首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用数组筛选器从csv中筛选数据未按预期工作

使用数组筛选器从csv中筛选数据未按预期工作
EN

Stack Overflow用户
提问于 2018-09-26 00:40:27
回答 1查看 27关注 0票数 0

我的数据以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

我正在尝试过滤数据,如下所示,但所有的过滤都不起作用。

代码语言:javascript
运行
复制
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的新手,所以我可能犯了一个基本的错误。

EN

回答 1

Stack Overflow用户

发布于 2018-09-26 00:59:09

执行与下面相同的操作可以达到预期效果,但数组筛选器不会。

代码语言:javascript
运行
复制
  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); });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52503032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档