Skip to content

Instantly share code, notes, and snippets.

@sarfarazansari
Created February 22, 2017 04:47
Show Gist options
  • Save sarfarazansari/a1288d41a050af25ee0b43fb629932b1 to your computer and use it in GitHub Desktop.
Save sarfarazansari/a1288d41a050af25ee0b43fb629932b1 to your computer and use it in GitHub Desktop.
check if 2 dimensional array is empty and undefined by using try catch
var arr1 = [
['A', 'B', 'C'],
['D', 'E', 'F']
];
var arr2 = [
['A', 'B', 'C'],
[]
];
var arr3 = [
['A', 'B', 'C']
];
function is2dArrayEmptyOrUndefined(_arr, _index1, _index2) {
try {
if (_arr[_index1] !== undefined && _arr[_index2] !== undefined && _arr[_index2].length > 0 && _arr[_index1].length > 0){
return false;
}
else{
return true;
}
}
catch(e) {
return true;
}
}
// should return FALSE
console.log(is2dArrayEmptyOrUndefined(arr1, 0, 1));
// should return TRUE
console.log(is2dArrayEmptyOrUndefined(arr2, 0, 1));
// should return TRUE
console.log(is2dArrayEmptyOrUndefined(arr3, 0, 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment