Working with empty objects in javascript array
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 try and pull grab 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 objects before the for loop? Or have the for loop not die when it
hits an empty object? Any help appreciated. Thanks.
No comments:
Post a Comment