Count unique occurrences of a particular string in a column

Count unique occurrences of a particular string in a column

woodsalexjwoodsalexj Posts: 21Questions: 6Answers: 0

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

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954
    Answer ✓

    You can use column().data() to get an array of the data in the column. You can chain toArray() 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

  • woodsalexjwoodsalexj Posts: 21Questions: 6Answers: 0

    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;

Sign In or Register to comment.