How to break or return from DataTable() each() function
How to break or return from DataTable() each() function
In my DataTable one of the column as unique values , I want to iterate through the column to find the one of the unique value & get the rowIndex , I tried using each() , I am able to get the rowIndex of the matched column value , but could not break the loop using return statement , is there any other way to break the each() function when a condition is met .I have 10,000 records so the each() function is iteration the 10000 records even when a match is found in 5th record.
Ref: https://datatables.net/reference/api/each()
Test code:
var count=0;
var temp="10021"
var table = $('#example').DataTable();
table.column( 0 ).data().each( function ( value, index ) {
count++;
if(value == temp)
{ console.log("matched rowIndex"+index); return true;
}
} );
console.log("count="+" count)
I always get count=10000 , even the matched column rowIndex is 5.
This question has accepted answers - jump to:
Answers
From the docs:
Further research reveals this note:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
Thank you tangerine, is there any other alternative effective way to get the rowIndex.
You could use
rows().indexes()
to get an array of indexes and then use a standardfor
loop to spin over it. You can then break out of that.Allan
Than you allan.