0

Trying to check if some variables contain things, because they throw errors and break the ajax function when they don't. Problem is that just checking if data[2][0] contains something causes the following error:

Uncaught TypeError: Cannot read property '0' of undefined

I'd very much prefer not to check this in a previous stage. How do I check if data[2][0] is defined, without causing the actual checking to break my js?

Code:

//ajax ^
success: function(data){
    var xp = data[0][0]; //Contains a string
    var yp = data[1][0]; //Contains a string
    var zp = data[2][0]; //Is not set, fails here

    if(xp === ''){ //Tried using null & undefined here aswell
       //Do nothing   
    } else {
        var one = data[0][0];
        var oneH = data[0][1];
        var oneS = data[0][2];
    }
    if(yp === ''){
        //Do nothing
    } else {
        var two = data[1][0];
        var twoH = data[1][1];
        var twoS = data[1][2];
    }
    if(zp === ''){
        //Do nothing  
    } else {
        var three = data[2][0];
        var threeH = data[2][1];
        var threeS = data[2][2];
    }
//ajax continues v

Any help will be much appreciated.

Algernop K.
  • 477
  • 2
  • 19
  • _Uncaught TypeError: Cannot read property '0' of undefined_ means that `data[2]` isn't defined, and maybe even that `data` isn't defined. – putvande Aug 30 '16 at 20:25
  • @putvande Yes, I know it isn't defined. `data[0]` and `data[1]` are both defined, they contain a link each. It's when they're undefined the site breaks, which is why I need to check if they are before using them – Algernop K. Aug 30 '16 at 20:27
  • You could do `var zp = data[2] && data[2][0] || null;` – putvande Aug 30 '16 at 20:29
  • 1
    If there's a possibility that `data[x]` might be undefined then you have to test that *before* you can try to use `data[x][y]`. (And test with `=== undefined`, not `=== ''`.) But you can still do the test in one line: `if (data[x] === undefined || data[x][y] === undefined) {...}`. – nnnnnn Aug 30 '16 at 20:33
  • @nnnnnn Thank you very much. I would have thought that `var zp = data[2][0];` would be defined similar to `var zp = '';`. Experimenting with this worked really well. – Algernop K. Aug 30 '16 at 20:45
  • JS has to evaluate the `data[2][0]` part before any assignment to `zp` can take place, and it has to evaluate `data[2]` before it can get to the `[0]`. So if `data[2]` is undefined then trying to use `data[2][0]` is basically saying "give me the first element of an array that doesn't exist", and that gives you an error... – nnnnnn Aug 30 '16 at 20:57
  • @nnnnnn Got it. Mind putting it in an answer so that I can commend you for helping me? – Algernop K. Aug 30 '16 at 21:02
  • I kind of don't want to post an answer because I feel like this is a duplicate of [this question](http://stackoverflow.com/questions/2631001/javascript-test-for-existence-of-nested-object-key) (although that question is talking more generally about objects rather than arrays it is conceptually the same thing). – nnnnnn Aug 30 '16 at 23:55

2 Answers2

2

You should check if a variable is undefined using typeof, not with an equality check against '':

if (typeof myVar === 'undefined')

You can also check if a variable is an array using Array.isArray(myVar)

Rob M.
  • 35,491
  • 6
  • 51
  • 50
  • @nnnnnn True, but their question was "checking for undefined causes undefined error", and that is because they are improperly checking for `undefined` AFAICT – Rob M. Aug 30 '16 at 20:38
  • I tried `typeof`. What you see up there is the result of an hour or so of experimenting. – Algernop K. Aug 30 '16 at 20:39
1

I would check to see if data[2] exists first, and then redefine zp if it does.

var zp = data[2];
if (zp) zp = zp[0];
larz
  • 5,724
  • 2
  • 11
  • 20