I have an array of arrays that I am trying to pull values from. The problem is that I get an undefined error when it hits an empty array.
Here is the array I'm working with:
data = [[Object { myvar=null}], [Object { myvar="testval"}], [], [], []]
Here is the javascript I'm using to get all myvar values:
myarr = [];
for (var i = 0; i < data.length; i++) {
console.log(data[i][0].myvar);
if (data[i][0].myvar) {
dep = data[i][0].myvar;
if (dep != null) {
myarr.push(dep);
}
}
}
The console.log looks like:
null
testval
Error: data[i][0] is undefined
So it breaks when it reaches the first []. How could I remove all the empty arrays before the for loop? Or have the for loop not die when it hits an empty array? Any help appreciated. Thanks.
dataArray is an empty Array. So when your reachi=2(which is the third element),data[i]is empty. So there is no `data[i][0]