d3过滤数据

时间:2017-10-29 14:24:23

标签: javascript d3.js

我有csv文件

Crime Category Description, Crime Code, Month Of Arrest
NONE, 123, 1
NONE, 234, 4
THEFT, 333, 4
THEFT, 444, 3
BATTERY, 456, 5
BATTERY, 55, 3

我尝试过滤数据"犯罪类别描述" =" THEFT"和 "犯罪类别描述" ="电池"。 为此,我使用

d3.csv("Urbana_Police_Arrests_Since_1988.csv", function(error,data){
...
.enter().append("rect")
.data(data.filter(function(d){return d.key = "THEFT" && d.key == "BATTERY" }))

不幸的是,它不起作用。任何想法为什么?

输出应为:

THEFT, 333, 4
THEFT, 444, 3
BATTERY, 456, 5
BATTERY, 55, 3

1 个答案:

答案 0 :(得分:0)

您希望将对象保留在Crime Category DescriptionTHEFT BATTERY,而不是THEFT { {1}},这是不可能的。

因此,请使用OR (||) operator



BATTERY

var csv = `Crime Category Description, Crime Code, Month Of Arrest
NONE, 123, 1
NONE, 234, 4
THEFT, 333, 4
THEFT, 444, 3
BATTERY, 456, 5
BATTERY, 55, 3`;

var data = d3.csvParse(csv);

var filtered = data.filter(function(d) {
  return d["Crime Category Description"] === "THEFT" || d["Crime Category Description"] === "BATTERY"
})

console.log(filtered)