0

Is there any way to check a two dimensional array's first dimension value existence, so for example

var groups = [][10];

// so now if "Student" exists in the first dimension I want to increment the second dimension
//if student is already added, increment it
groups["Student"] = groups["Students"] + 1;

// else
groups.push(["Student",0]);
Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105

2 Answers2

0

You can do:

  if (typeof groups["Student"] != 'undefined') {
       groups["Student"] += 1;
  }
  else {
       groups["Student"] = 0;
  }
zozo
  • 8,230
  • 19
  • 79
  • 134
0

The example you provide seems to be wrong.

Firstly of all a 2D array will be instantiated something like:

var items = [[1,2],[3,4],[5,6]];
alert(items[0][0]); // 1

See: How can I create a two dimensional array in JavaScript?

Secondly, you cannot access an array by using keys. You are just creating properties in your array object. So:

groups['Student'] === groups.Student  // true

If you go for this approach then, you must consider the following:

groups['Student'] = undefined; // Your dimension is created
groups.hasOwnProperty('Student'); // true
typeof groups['Student'] == 'undefined' // true

It may be a good idea to consider using only arrays, or just work with objects.

Community
  • 1
  • 1