I am trying to do duplication checking on names while adding the records. Initially, I have assigned value to false. In case any mismatch, value is being changed to true inside the each loop and exit the loop. But outside the loop, value again shown as false.
function IsDuplicated(name) {
var value = false;
masterTable = $('#example').DataTable();
var form_data = masterTable.rows().data();
$.each(form_data, function (key, value) {
alert(value.Name);
if (name== value.Name) {
value = true;
alert(value+ " 1")
return false;
}
});
alert(value+ " 2")
return value;
}
Can you help why it is changing to default value after returning from the each loop?
It's cleaner if you do that using find instead
function IsDuplicated(name) {
const masterTable = $('#example').DataTable();
const data = masterTable.rows().data();
return !!data.find(d => d.Name == name)
}