Nested Arrays in javascript

A lot of nested arrays are written like this:

var ourPets = { 
  "cats": [
    "Meowzer",
    "Fluffy",
    "Kit-Cat"
  ],
  "dogs": [
    "Spot",
    "Bowser",
    "Frankie"
  ]
};
ourPets.cats[1]; // "Fluffy"
ourPets.dogs[0]; // "Spot"

OR

var myPlants = [
  { 
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  },
  {
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  }  
];

Why do people put “objects” inside arrays? I know arrays is a subtype of object, but why people dont write it this way? Or may I ask can we write it in this way? If so, when we can do it and what’s the difference between using [] only and using [] and {} at the same time?

var myPlants = [
  [    // changed from { to [
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  ],
  [
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  ]  
];

Let’s clear things out.

The first code snippet you wrote is not nested array.

[ [...], [...], [...], ... ]

This is nested array, not { key1: [...], key2: [...], ... }

This isn’t a nested array, as well

[ {...}, {...}, {...}, ... ]

This is an array of objects.

Array is indexed, which means you access them by non-negative integers. If it is accessed by arbitrary key like a string, it is not an array.

Now, why do people put objects inside array?
Why not? There are many practical examples.
e.g) items in a cart, players waiting for their turn, whatever data object in a stream, and etc…
Although, not all of them may use array as the underlying data structure, putting objects in some container is not an odd concept.

Also, why not people write in your way? Because having the same notation for array and object is confusing to parse and confusing to use. Having ambiguous syntax is not a good property of a programming language.

Sorry but really, you gotta start typing your hypothesis into console. What you wrote is not valid JS syntax, which answers rest of the question.

1 Like

which part? could you be more specific?

so it is common to use [ {...}, {...}, {...}, ... ] and { key1: [...], key2: [...], ... } but not this[ [...], [...], [...], ... ] ?

#1 This part is not valid JS syntax.

var myPlants = [
  [    // changed from { to [
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  ],
  [
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  ]  
];

#2 It is common to use any of those forms, the choice of data structure is is yours.
The first, very common

players = [ // Array of objects
    new Player(...), // which results {...}
    new Player(...),
    ...
]

The second, very common, (but not all values need to be array, because container is object)

customer_history = { // object where eacy key is an array
    orders: [ order1, order2, order3, ... ],
    refunds: [ refund1, refund2, refund3, ...]
} 

The third, still common

grid = [
    [1, 2, 3],
    [4, 5 ,6],
    [7, 8, 9],
    ...
]

points = [ // only if point is simple (x, y)
    [x1, y1],
    [x2, y2],
    ...
]

Think of a database, or a spreadsheet, or a similar exceptionally common table based thing for storing lots of data.

You have rows, which have columns of data, and each of those columns has an identifier.

[ { name: 'Foo', age: 12, location: 'somewhere' }
, { name: 'Bar', age: 25, location: 'somewhere else'}
, // etc
]

Or

{ rowId1: { name: 'Foo', age: 12, location: 'somewhere' }
, rowId2: { name: 'Bar', age: 25, location: 'somewhere else'}
, // etc
}

Arrays of arrays doesn’t make much sense for most common things, it’s a pretty useless data structure in most cases. It is useful, but for specific things like matrices of numbers, not for general use.

Arrays are an ordered collection of things, and generally those things are the same type (otherwise it becomes difficult to use them). Objects are an unordered map of keyed data, where the keys are always strings. You don’t generally access arrays directly by their key (the index), you iterate over them. Whereas objects you access directly by key (you can iterate but it’s not that useful).

So if you have an array of objects, you can iterate over them and then access each object, which is normally going to have the same structure. Or if you have an object with id keys (which, if it was nomalized, would also have another field containing an array of those keys), and you iterate over the set of keys or access them directly

1 Like

i see. thanks! (20 character thingy)

1 Like