tomtomtom
tomtomtom

Reputation: 1522

D3.js ~ How to filter data values containing certain values/words?

filtering data for a visualization I noticed that some values that do not correspond exactly to the values contained in the filter are not passed. what I would like to do is to filter all the values in my dataset that contain that word, now i have:

stile.filter(function (d) { return d.StyleName == "Bold" || d.StyleName == "Demi" || d.StyleName == "Semibold"; });

I thought that adding the .contains() at the end of each value I want filtered could do the trick, like so:

stile.filter(function (d) { return d.StyleName.contains("Black") || d.StyleName.contains("Heavy") || d.StyleName.contains("Extrabold") || d.StyleName.contains("BoldNonextended"); })

But this didn't work unfortunately. Any help as always is really appreciated, thanks!

EDIT: my dataset is a csv, the StyleName column has vairous values, like: Bold, Bold Extended, Regular and so on. If I use the filter function that I have now, the data row containing Bold Extendend for example, is not returned because it has the word Extended also in it. That's what I'm trying to solve

Upvotes: 0

Views: 3489

Answers (1)

Jonah
Jonah

Reputation: 16242

Instead of contains try using match() and regular expressions:

stile.filter(function (d) { return d.StyleName.match(/Black/) || d.StyleName.match(/Heavy/) || d.StyleName.match(/Extrabold/) || d.StyleName.match(/BoldNonextended/); })

Upvotes: 4

Related Questions