1

I want to check the existence of arr[i][j]. So I did the following test :

typeof arr[i][j] !== 'undefined'

The problem is that if arr[i] is not defined, I get an error. So I did it like this :

typeof arr[i] !== 'undefined' && typeof arr[i][j] !== 'undefined'

Is there a better way to do this ?

chado
  • 49
  • 7
  • You don't need `typeof`. If the value is not defined, then you can check like `arr[i] !== undefined && arr[i][j] !== undefined` – Vigneswaran Marimuthu May 13 '15 at 10:30
  • what not like in _typeof arr[i] !== 'undefined' && typeof arr[i][j] !== 'undefined'_? also you need add checking to _arr_, because if _arr_ is `null` or `undefined` it also raise exception – Grundy May 13 '15 at 10:31
  • In fact, `arr[i] !== undefined && arr[i][j] !== undefined` worked fine, thanks. Is there a way to check `arr[i][j]` in one test ? – chado May 13 '15 at 10:35
  • @chado, **note**: if _arr_ is `undefined` you still get exception – Grundy May 13 '15 at 10:39

5 Answers5

0

This will solve part of your question: How to check for "undefined" in JavaScript?

But of course, if you want to check for the existance of arr[i][j], first you have to check for the existence of arr[i], or you'll get an error. For example:

if(arr[i] != undefined && arr[i][j] != undefined){
   //your code
}
Community
  • 1
  • 1
Vandervals
  • 5,774
  • 6
  • 48
  • 94
0
if(arr[i] != null && arr[i][j] != null)
Michele d'Amico
  • 22,111
  • 8
  • 69
  • 76
0

if(arr && arr[i] && arr[i][j]) is the idiomatic way to do it - checking explicitly for null, undefined or other falsy values doesn't add any value here.

joews
  • 29,767
  • 10
  • 79
  • 91
  • but sometimes _0_ or _empty string_ or even _false_ is valid value, but by your way it return _false_ – Grundy May 13 '15 at 10:49
  • Sure, and `1` and `true` are truthy, but the subscript `[ ]` syntax will still fail. If you want to do any type checking, check that `arr` and `arr[i]` are really Arrays. Most JavaScript code is written with the assumption that you will provide values of the correct (duck) types, or get a `TypeError`. – joews May 13 '15 at 11:36
0

Write your utility function

function arrayItemCheck(array, i, j){
  return array != null && typeof array[i] != 'undefined' && typeof array[i][j] != 'undefined';
}

var arr = [
    [1],
    [1,2]
];

console.log(arrayItemCheck(arr, 2, 0));
SharpEdge
  • 1,782
  • 1
  • 10
  • 20
0

You cannot achieve this with native JavaScript. But there are many different ways to easily achieve this. Here's one:

var isset = function isset (arr) {
  var indexes = Array.prototype.slice.call(arguments, 1);
  var current = arr;

  for (var i = 0; i < indexes.length; i += 1) {
    arr = arr[indexes[i]];

    if (typeof arr === 'undefined') {
      return false;
    }
  }

  return true;
}

Then you can use it like this:

var arr = [1, [11, 22, [[1111, 2222, 4444], 222, 333]], 3];

console.log(isset(arr, 0, 1)); // -> false
console.log(isset(arr, 0, 4, 4, 6, 7, 8)); // -> false
console.log(isset(arr, 1, 2, 0)); // -> true

if (isset(arr, 1, 2, 4)) {
  // ...
}
avetisk
  • 11,651
  • 4
  • 24
  • 37