I am having a weird problem here.
I am trying to check if the variable exists
I have
for(var i=0; i<6; i++{
if(results[(i+1)].test){
results[(i+1)].test=i + 'test';
}
}
I know results(6).test is undefined and I need to add that extra index to check if the variable exists. I keep getting console error saying
Uncaught TypeError: Cannot read property 'test' of undefined
I thought if(results[(i+1)].test) would check if the variable exists for me
I also tried
if(typeof results[(i+1)].test !='undefined'){
results[(i+1)].test=i + 'test'
}
but still getting error. How do I fix this?
Thanks a lot!
You’re checking whether foo.test is undefined, but your problem is that foo itself (in this case
results[i + 1]) is undefined.You need to check that first, eg.: