Count unique occurrences of a particular string in a column
Count unique occurrences of a particular string in a column
First I got a list of all the unique occurrences of a string in a particular column.
const labels = table
.column(1)
.data()
.unique();
How then do I get the number of times that string actually occurs in the column? Something like:
for (let i = 0; i < labels.length; i++)
{
var numberOfTimesStringAppears = table.columns(1).search(labels[i]).count();
}
This question has an accepted answers - jump to answer
Answers
You can use
column().data()
to get an array of the data in the column. You can chaintoArray()
to make the result a pure Javascript array if needed. From there you can use Javascript techniques to count the number of occurrences for each element. See if any of the techniques in this SO thread work for you.Kevin
Thanks,
solution is:
var tableDataArray = table.column(1).data().toArray();
get that first then in the loop:
var numberOfTimesStringAppears = tableDataArray.filter(x => x === labels[i]).length;