Reputation: 17
I have the csv file
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
I try to filter the data where "Crime Category Description" = "THEFT" and "Crime Category Description" = "BATTERY". To do this, I use
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" }))
Unfortunately, it does not work. Any ideea why?
Output should be:
THEFT, 333, 4
THEFT, 444, 3
BATTERY, 456, 5
BATTERY, 55, 3
Upvotes: 1
Views: 2177
Reputation: 102194
You want to keep the objects where Crime Category Description
is THEFT
or BATTERY
, not THEFT
and BATTERY
, which is impossible.
Therefore, use the OR (||) operator:
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)
<script src="https://d3js.org/d3.v4.min.js"></script>
Upvotes: 1