0

I am looking for solution how to break every() function or replace it with cycle for. I tried to use command break and return but it didn't work. Maybe I should try to make an exception and stop cycle with this.

Is possible to break this loop?

Is possible to replace this function with cycle for?

Here is an example of code:

table.rows().every( function ( rowIdx, tableLoop, rowLoop ) {
    var data = this.data();
    // ... do something with data(), or this.node(), etc
} );
hard456
  • 15
  • 1
  • 5

2 Answers2

1

Here is my solution:

function loopTableRows(team) {
    var table = $('#myTable').DataTable();
    var numberOfRows = table.data().length;
   
    for (var i = 0; i < numberOfRows; i++) {
        //get data from row
        var data = table.row(i).data();
        if (data[0] == team.id) { //test cell for value
            return true; //break cycle
        }
    }
    return false;
}
hard456
  • 15
  • 1
  • 5
0

Array.prototype.every looks to be different than what you want:

The every method executes the provided callback function once for each element present in the array until it finds one where callback returns a falsy value. If such an element is found, the every method immediately returns false. Otherwise, if callback returns a truthy value for all elements, every returns true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

var tableRows = [1, 2, 3, 4, 5, "word"];
var tableRows2 = [1, 2, 3, 4, 5];

var everyTest = tableRows.every(function(currentValue, index, array) {
  return (typeof currentValue == "number")
});

var everyTest2 = tableRows2.every(function(currentValue, index, array) {
  return (typeof currentValue == "number")
});

console.log(everyTest);
console.log(everyTest2);

You probably wanted to use an Array.prototype.forEach. Sadly there is no way to break from a forEach short of throwing an exception, which sounds like your dilemma.

var tableRows = [1, 2, 3, 4, 5, "words"];

var forEachTest = tableRows.forEach(function(currentValue, index, array) {
  console.log(currentValue);
});

Although forEach doesn't have a break, the traditional for loop does!

var tableRows = [1, 2, 3, 4, 5, "words", 6, 7, 8];

for (var i = 0; i < tableRows.length; i++) {
  var current = tableRows[i];
  if(typeof current != 'number')
    break;
  console.log(current);
}
Hodrobond
  • 1,665
  • 17
  • 18