-1

If I have an array like...

outerArray = [
    middleArray1 = [
        innerArray1 = ['data'],
        innerArray2 = ['more data']
    ],
    middleArray2 = [
         ...more stuff...
    ]
];

How can I access one of the inner arrays in javascript?

Is it outerArray.middleArray1.innerArray1 ?

dedman
  • 868
  • 6
  • 16
Soup
  • 47
  • 7
  • In your case its actually just `innerArray1`. But that's because your definition is weird (you are assigning inside your array definition). – TiiJ7 Jun 22 '18 at 14:51
  • Your "thing" is actually a 3D array, if it is even one, because it looks more like an object – Sebastian Speitel Jun 22 '18 at 14:52
  • in your example you can access in your "middle array" with an index, like outerArray[0] .... outerArray[N-1], but your syntax is ... weird actually – Margon Jun 22 '18 at 14:53
  • You can't access arrays by properties. Use `[]` brackets to access data. Do `console.log(outerArray);` and look at the indices of each level. – Alex Jun 22 '18 at 14:56
  • 1
    Is this actually how you've defined your data? If so, you should seriously reconsider. If you're instead trying to simply describe your array structure using this format, have a look at how to create a [Minimum, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) and edit the question with real code. This will be much more likely to produce a helpful answer to your question. – Fissure King Jun 22 '18 at 14:57
  • 1
    Pretty much you should just get rid of the naming/assigning portion within the array(s) (`middleArray1 = ...`) as it's going to just end in indexes anyway. – Matt Jun 22 '18 at 14:59

4 Answers4

2

You haven't really told us what you are trying to do here, but I suspect you are mixing up objects, which use {} and have property access like outerArray.middleArray1, and arrays which are defined with [] and accessed by index: outerArray[0].

You example sort of works because assignments return the assigned value. This means something like:

middleArray1 = [
    innerArray1 = ['data'],
    innerArray2 = ['more data']
]

is the same as:

middleArray1 = [['data'],['more data']]

with the side effect that innerArray1 and innerArray2 get defined as global variables. For example:

outerArray = [
    middleArray1 = [
        innerArray1 = ['data'],
        innerArray2 = ['more data']
    ],
    middleArray2 = [
        ' ...more stuff...'
    ]
];

// log global variable innerArray1 (probably not what you want)
console.log(innerArray1)

// access ith index:
console.log(outerArray[0][0])

This is almost certainly not what you want. If you want to access data with properties like outerArray.middleArray1, you need to define an object. For example:

// use {} to define object literal
outerArray = {
        middleArray1: {
            innerArray1: 'data',
            innerArray2: 'more data',
        },
        middleArray2: ' ...more stuff...'
 };
 
 // now access by property name
 console.log(outerArray.middleArray1.innerArray1)

Of course, you probably want to use a better naming convention for your properties…

Mark
  • 90,562
  • 7
  • 108
  • 148
1

First of all, that's very weird way of creating nested arrays as you may not be able to access array elements with array names like 'middleArray1', I tried that but it doesn't work. After all, the array will be available in this format only...

outerArray = [
    [
        ['data'],
        ['more data']
    ],
    [
         ...more stuff...
    ]
];

You could still access inner elements as outerArray[0][0] (innerArray1) or also

outerArray[0][0][0] (1st element of innerArray1)

dedman
  • 868
  • 6
  • 16
1

Continuing on what deadman said.

Let's break down your multi-dimensional array into levels.

  1. middleArray is Level 1
  2. middleArray -> innerArray1 is Level 2
  3. middleArray -> innerArray1 -> value is Level 3

To access data for example, you will have 3 levels meaning 3 [] brackets.

Example:


var data = outerArray[0][0][0] will output data.

var moreData = outArray[0][0][1] will output more data.


outerArray[0] accesses the first array on Level 1 which is middleArray1.

outerArray[1] accesses the second array on Level 1 which is middleArray2.

outerArray[0][0] accesses the first array on Level 2 which is innerArray1.

and so on.


Read over Creating a Multidimensional Array
Alex
  • 2,164
  • 1
  • 9
  • 27
1

Your declaration is a bit strange as noted by TiiJ7. Typically an array of arrays would look more like this:

const outerArray = [
    [
        [data],
        [moredata]
    ],
    [
        // more stuff
    ]
]

And you would call each item by its index like outerArray[0][0][0]

It looks like what you really want is an object.

const dataObj = {
    middleObj1: {
        innerObj1: [data],
        innerObj2: [moredata]
    },
    middleObj2: {
        // more stuff
    }
}

This one can be called by key instead of index alone. That is, you can either choose to use dataObj['middleObj1']['innerObj1'][0] or dataObj.middleObj1.innerObj1[0]

Notice how I'm still ending it with an index in both cases. That's because I kept your innermost array in my example. You can nest arrays within objects. In fact arrays ARE a special type of object in JavaScript. JavaScript just uses generic objects instead of multi-dimensional Arrays.