FAQ: Objects - Nested Objects

That is in-context, meaning it is a literal property which is assigned via the colon.

The above is not in context and would have to be written outside of the object literal.

I did step 2 as:

spaceship.passengers = [
{‘Passenger 1’: {name: ‘Luis’, surname: ‘Kells’}},
{‘Passenger 2’: ‘Karol’}

Does this means both are anonymous objects, but then the first one is another object named Passenger 1 with keys name and surname while Passenger 2: Karol is key-value?

We shouldn’t have to go to the trouble of naming the anonymous objects. They can be got (polled) by their array index.

  {name: 'Luis', surname: 'Kells'},
  {name: 'Karol', surname: 'Klein'}
1 Like

why i cannot access psngr1 ?? can’t we declare new object inside array ?

I am relieved to find that I am not alone with my frustration in JavaScripts Objects lesson 7 of 10, instruction 2 of 3. “Right now the passengers property has a value of null . Instead, assign as its value an array of objects. These objects should represent the spaceship ‘s passengers as individual objects. Make at least one passenger object in the array that has at least one key-value pair on it.” I ran my code and triggered this response " Does the passengers array contain at least one passenger object? ". My code, spaceship.passengers = [‘name1’, ‘name2’, ‘name3’, {name4: ‘Space Dog’}] My code returned in Node.js v12.19.0. is
[ ‘name1’, ‘name2’, ‘name3’, { name4: ‘Space Dog’ } ]. My code seems to work fine. How do I get past this to move on to 3 of 3 in lesson 7 of 10?
OKAY OKAY What good is a key:value pair without a value? Still runs without but probably not well. Thank you forum

Your array has 3 string literals followed by an object. Likely, the SCT is testing the first element of the array. Every element of the array should be an object rather than a string literal (which technically is an object, but not what the SCT is checking for).

> const spaceship = {}
> spaceship.passengers = ['name1', 'name2', 'name3', {name4: 'Space Dog'}]
[ 'name1', 'name2', 'name3', { name4: 'Space Dog' } ]
> typeof(spaceship.passengers[0])
> typeof(spaceship.passengers[3])

Sorry for the late response. This was GOLDEN! Thanks so much.

1 Like

So, like many other people I had a lot of trouble with the passengers question. I kept trying to write a definition at the bottom of the editor that would change the value of passengers at the top. At first I thought the question was badly worded and that was the cause of my confusion, but then I read it again and the phrase ‘assign as its value’ became more obvious. If we’re reassigning the value, we’re changing the original rather than mutating it with another definition, right?

Please someone tell me if I’m right or wrong here, because this is how I understood it to complete the task by looking to replace the object at the top rather than change it from the bottom of the editor.

So after trying various different methods and then coming here and looking at other solutions and explanation, I finally cracked creating multiple Objects with their own key pairs:

passengers: [
            name: 'Keith', 
            travellingFor: 'business'
            name: 'Susan', 
            travellingFor: 'Business'

I know this can all be written on one line, I just split it like this just to make it easier to read and understand while getting to grips with it.

My question is though, is that at the core of it, this is also a nested array, so the object at index[0] of passengers also holds 2 indexes, (name and travellingFor), so in theory, this should work:


but instead it prints undefined but:


Prints ‘Keith’ as expected.

Is it just because the array contains Objects as opposed to just values, or have I gotten the whole nested array part wrong?

The example above is not a nested array. The array is linear, not 2D. The elements of the array are each an object.

Objects do not have an index. They have keys. That is why your first example does not work. We must access the content by their key.

1 Like

Right that makes sense, I just thought from the fact we’re storing it in an array, it would be a nested array.
So the reason we’re able to call index[x] to begin with to get the name is cause even though it’s an object stored there, we have created an array with objects, and just need to call the key to access the value of index[x]

1 Like

The array is used here for grouping. It is much simpler than having a passengers object. All the objects have that one thing in common, they are all passengers.

const spaceship = {
  passengers: [
            name: 'Keith', 
            travellingFor: 'business'
            name: 'Susan', 
            travellingFor: 'Business'
const passengerIndex = []
let i = 0
for (let x of spaceship.passengers) {
  passengerIndex.push([x.name, i++])
console.log(passengerIndex)    //  [ [ 'Keith', 0 ], [ 'Susan', 1 ] ]
const directory = Object.fromEntries(passengerIndex)
console.log(directory)    //  { Keith: 0, Susan: 1 }
{ name: 'Keith', travellingFor: 'business' }
1 Like

Can anyone explain how to add an extra value on the array if spaceship = {
passengers: [{name: [‘Pluto’] will be correct but if I try spaceship = {
passengers: [{name: [‘Pluto’, ‘Toystory’ }] and try to assign element of 1th instead of 0th I will get undefined as result.

Hope my question make sense because reading all the previous answers made me even more confused ;-(

Check your brackets. Both examples have bracket errors.

Start from the outside…

passengers: []

A passenger object will look like,

{name: 'Pluto', role: 'Navigator'}

Thank you very much for your help. I managed to understand the mistake I made.

1 Like

Is it possible to assign a name to an object that is part of an array? The solution given only had the key: value.

passengers : [{name: 'Space Dog'}]

But how do we write the code if we were to use an object name? Something like below but as an object inside the array.

let passenger1 = {
     name: 'Space Dog'

Hi, I’m not sure if someone has already asked this question, but I’m finding it troublesome for me to understand why this is an incorrect syntax:

spaceship.passengers = [human: {name: 'Mark}];

Why exactly we can’t name our object in an array? Also, if we can’t name it, can we call it an anonymous object?

Not sure what you’re after here exactly, but first of all, you’ll need to add a ' to the end of your string literal. Beyond that, like I said, I’m not sure what it is you are wanting to do. Here are a couple of examples of what you may be trying to accomplish:

const human = {name: 'Mark'}; const spaceship = {}; spaceship.passengers = [human]; spaceship.passengers.push({klingon: {name: 'Warf'}}); console.log(spaceship.passengers); // We can still reference 'Mark' with the variable human console.log(human); // We can only reference 'Warf' by his position in the array console.log(spaceship.passengers[1]); // If we wanted only his name console.log(spaceship.passengers[1].klingon.name);

Meanwhile I was just stuck on this assignment step for a full 10 minutes because I write in Brittish. :expressionless:
“favourite” instead of “favorite”…
“favorite” looks like a misspelling to me lmao :joy: