我有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
答案 0 :(得分:0)
您希望将对象保留在Crime Category Description
为THEFT
或 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)