2

I have a 2-dimensional array that I want to check the bounds in Javascript. I prefer doing this without checking each index independently.

For example if my 2d array is...

dataset[row0-1][column+column0]

I really do not want to have to do the following...

if(row0-1 >0)
{
    if(dataset[row0-1].length >= column+column0)
    {
        data = {label : dataset[row0-1][column+column0]};
    }
}

The problem is that I have to first check the row and then check the column. I prefer doing both of these checks with one operation or on one line. Rather then returning an out of bounds error, why doesn't Javascript just return null or undefined?

For example, in a future version of Javascript, wouldn't it be better if we could do this?

if(dataset[row0-1][column+column0] != undefined)
{
   data = {label : dataset[row0-1][column+column0]};
} 
else 
{
    ...
}
4
  • Is it really that hard to add one more check on the same line. Just check that the row and then column are both defined in one if statement. if you are already going to write one it's not that much more to add one more statement. Commented Jul 9, 2012 at 16:20
  • Its maybe a bit complicated to correct this, it would help if javascript returned an undefined rather then crashing after a bad lookup maybe we could add a .prototype that would prevent a crash for array lookups?!? I can't remember if it crashed or not, its been a while. Commented Mar 13, 2015 at 4:50
  • row = 5 5 dataset = [0,1,2,3,4] Array [ 0, 1, 2, 3, 4 ] dataset.length 5 dataset[6] undefined dataset[6][1] TypeError: dataset[6] is undefined dataset[5][1] TypeError: dataset[5] is undefined dataset[4][1] undefined Commented Mar 13, 2015 at 4:54
  • Looks like this has been corrected. Commented Mar 13, 2015 at 5:00

5 Answers 5

2

You can write a function to check:

function CheckArrayIndex(x, y) {
    if (dataset.length > x && dataset[x].length > y) {
        return dataset[x][y];
    }

    return null;
}

Then you can use it like this:

if(CheckArrayIndex(row0-1,column+column0) != null){
   data = {label : dataset[row0-1][column+column0]};
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think the reason for checking each index independently is because each dimension can have different lengths on each index.

I know C# has Multidimensional Arrays, but Javascript uses Jagged Arrays.

I do not picture this changing any time soon.

1 Comment

from one perspective this is considered a bug with Javascript? It seems so fundamental even in 201X?
0

You can easily combine nested if-clauses to a one-liner using the AND-operator &&:

if (row0-1 > 0 && dataset[row0-1].length >= column+column0){
    data = {label : dataset[row0-1][column+column0]};
}

although you really should check for the array's length, not (only) greater-than 0. Better:

if (row0-1 > 0 && row0-1 < dataset.length && column+column0 < dataset[row0-1].length)
    data = {label: dataset[row0-1][column+column0]};
}

Comments

0

I do mostly like this: say we have 2d array arr and we need to access an element which isn't there.

var arr = [/*elements*/]
var get = function(x, y) {
  if(!arr[x]) {
      return false;
  }
  return arr[x][y] || false;
}

Comments

0

Using the @dmck answers, I use prototype in Array object.

Function:

Array.prototype.CheckBounds = function (x, y)
{
    if (this[x] === undefined)
        return false;

    if (this.length > x && this[x].length > y) {
        return true;
    }

    return false;
}

How to use:

if(myArray.CheckBounds(5,10)) //Check bounds
 {
   //OK
 }else{
   //Out of bounds
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.