2

So, I know what I am trying to do I am just not sure if I am doing it right, and hopefully my question describes what I am trying to do.

I have about a dozen arrays that look like this:

var floorStatusAr = ['','','','','','','','disabled'];
var floorStatusSp = ['','','','','disabled','','',''];
var floorStatusSmm = ['','disabled','','','','','disabled',''];

It seemed too redundant to me, so I was hoping there was a better way. This was my attempt:

var floorStatuses = {
    AR:['','','','','','','','disabled'],
    SP:['','','','','disabled','','',''],
    SMM:['','disabled','','','','','disabled','']

I've also tried:

var floorStatuses = {
    "AR":['','','','','','','disabled',''],
    "SP":['','','','','disabled','','',''],
    "SMM":['','disabled','','','','','disabled',''],

Both ways I can get results from

alert(floorStatuses.AR[0]); //alerts "", [7] alerts "disabled"

But in the rest of my code, I need to be able to call these dynamically. To test this I do:

initial = "AR";
alert(floorStatuses.initial[0]);

But I just get an error that floorStatuses.initial is undefined.

If I could even capture "AR" from the array such as

alert(floorStatuses[0]); //hoping to alert "AR", but it just alerts "undefined"

I could work with that, because then I could run a for loop and compare it with another variable.

I must just have syntax or logic wrong or something. Surely this is possible.


To address this being marked as a duplicate:

If I came across that question in my searching for the solution to this question, it would not have solved my problem. The question in the duplicate does not even use the same type of object as mine and thus it would not explain how I am to call what I am trying to call. One glance at it and I'd move along because my object contains arrays and so looks nothing like the duplicate's.

Christine268
  • 722
  • 2
  • 13
  • 32
  • Short answer: you want to say `floorStatuses[initial][0]`. – Paul Roub Aug 11 '15 at 17:58
  • @PaulRoub I wouldn't think so. I've accessed properties of objects/arrays plenty in my code. This, however, was a little bit trickier for me as the array is set up differently. If I managed to come across that question in my search, it would not solve this particular problem nor point me in the right direction. – Christine268 Aug 11 '15 at 18:02

2 Answers2

2

You're so close:

alert(floorStatuses[initial][0]);
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Aye I am. Thank you ever so much. I knew I was close. I see now why that makes sense. It is almost like its an array of arrays. Would this be called a 2-dimensional array? – Christine268 Aug 11 '15 at 18:00
  • 2
    No. `floorStatuses` is not an array, it's an object. That object has several arrays as properties. – Paul Roub Aug 11 '15 at 18:06
  • @Christine268: It would be better to think of an array as an object that has properties that are numbers. – Scott Hunter Aug 11 '15 at 18:52
  • @ScottHunter Hmm, that does give it a different perspective. Normally I think of objects as arrays with properties! Objects work very similarly arrays do after all. – Christine268 Aug 11 '15 at 20:58
2

In JavaScript you can access object properties in two ways, either object.propertyName or object["propertyName"]. You can read more about working with objects here: MDN

What you need to do is this:

initial = "AR";
alert(floorStatuses[initial][0]);
Johan Karlsson
  • 6,419
  • 1
  • 19
  • 28
  • I love the objects reference that you linked. Objects are so powerful beyond what I've yet to use them for. I especially like how objects can contain calls to functions or even apparently other objects. – Christine268 Aug 11 '15 at 18:08