Question About Arrays with Objects

Tell us what’s happening:
I have just finished Basic JavaScript: Accessing Nested ArraysPassed and was wondering if objects in the array can given a name like for example an object with a nested object

var object = {
  **prop1:** {
    albumTitle: 'Slippery When Wet',
    artist: 'Bon Jovi',
    tracks: ['Let It Rock', 'You Give Love a Bad Name']
  },
  **prop2:**  {
    albumTitle: '1999',
    artist: 'Prince',
    tracks: ['1999', 'Little Red Corvette']
  }
}

vs

Your code so far


// Setup
var myPlants = [
**no name for the objects**{
  type: "flowers",
  list: [
    "rose",
    "tulip",
    "dandelion"
  ]
},
{
  type: "trees",
  list: [
    "fir",
    "pine",
    "birch"
  ]
}
];

// Only change code below this line

var secondTree = ""; // Change this line

Is it because of the way array are accessed? U use numbers to access them so I guess it wouldn’t be a point. But if that is the case wouldn’t it be better to just use objects with nested objects over Arrays because you have the title that can help you know what you are accessing?

Could you give an example of when you would used an Array with nested objects over an object with nested objects?

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Accessing Nested Arrays

Link to the challenge:

if the names you are going to use are stuff like prop1, prop2, prop3, etc
an array is better as you can iterate over an array more easily
if you have a data structure that needs to be filtered or sorted, array lets you do that
it’s easier to add elements to an array dinamically than to an object too

if you use an API to call data from somewhere, you have no control
on the structure of that data, so you still need to know how to deal with that

1 Like

Thank you so an array is used when the names aren’t that important and you want to access the data quick or easily vs an object. I don’t know what an API is but it sounds interesting.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.