Count rows depending on a criteria
Count rows depending on a criteria
microshop
Posts: 11Questions: 4Answers: 1
Hi,
I want to count the rows of a column, but only count if matches the criteria.
For example, we have that drawed table:
Name-Hired
James-Yes
Olivia-Yes
Paul-No
Sonya-Yes
Donna-No
The criteria is: Hired = Yes. And the result is Count = 3.
But I don't want draw anything, only count.
Thank you.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You can use
filter()
for this. The length of the result will be theCount
.Kevin
Hi Kevin,
I've used the example of
filter()
but it returns always 0.If I console.log filteredData, or if I add .count(), always returns 0.
That suggests there is no data point in the first column which is > 20. Are you able to link to an example showing the issue so we can see the data as well?
Allan
For your case this should be:
And
Should be the column containing the "Yes" / "No" values. In your example this would be:
Resulting in this:
Kevin
I've made a simple example and it works, then should be my own code.
But, if I do a search, the filter doesn't apply based on the search.
https://jsfiddle.net/e2098fcz/1/
You need to use a
selector-modifier
to control which data is looked at. You would use something like this to filter only on the search results:The
column()
docs have more info.Kevin
Thank you very much, I've made some changes and now it works in the simple example.
https://jsfiddle.net/e2098fcz/3/
I've improved the script for search in several columns.
https://jsfiddle.net/e2098fcz/6/