Object inside an object, proper syntax, help


#1

as can be seen here..

https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/1?curriculum_id=506324b3a7dffd00020bf661

we are building steve and bill objects inside friends. and as can be seen in the link, bill and steve objects are closed with a semicolon tag.

then come to this exercise..

https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/3?curriculum_id=506324b3a7dffd00020bf661

we need to build steve and bill inside friends. so when i am trying to use semicolon to close bill and steve object, i.e.

var friends=
{
    bill:{};
    steve:{};
};

i am getting syntax error..
i know that using comma lets you complete this exercise. but i would like to know the proper syntax for closing objects(or proper syntax for closing objects inside objects).


#2
var friends=
{
    bill:{};
    steve:{};
};

friends is an anonymous object in an assignment statement. The syntax is correct on this part.

bill and steve are properties in object literal syntax, so are separated by commas. Their values are also anonymous objects, but the semi-colon syntax does not apply.

{ key1 : value1, key2 : value2 }

The only time we use a semi-colon after a closing brace, }; is when there is an assignment:

var obj = { key: value };

var func = function () { // code statements };

When we write,

bill: { }

we are declaring an association, not an assignment. The object is associated with the bill key.


#3

thank you.

1- so basically, bill is a key here, which is again going to store some keys and values(like an object) since we added {}
is that correct?


#4

That is correct. bill and steve are what I call primary keys of the friends object. Their values are both objects, which will, like all objects, also have key: value property pairs.

We may associate any primitive or object to a key:

var mixedObj = {
    number: 1,
    string: "String",
    boolean: true,
    array: [],
    object: {},
    method: function () {}
};

#5

@mtf
perfect. thank you.very much. this sounds like associative arrays from PHP..


#6

JavaScript objects are very much like associative arrays in PHP, especially when we view them in subscript syntax:

var mixedObj = {};

mixedObj['number'] = 1;
mixedObj['string'] = "String";
mixedObj['boolean'] = true;
mixedObj['array'] = [];
mixedObj['object'] = {};
mixedObj['method'] = function () {};

In an IDE with an active linter, these lines will all have a caution (excepting the var statement):

... better written in dot notation

It's still valid, though, just not very common. We see this relationship in the for..in statement...

for (var key in object) {
    console.log(key + ": " + object[key]);
}

#7

@mtf
alright, thank you very much sir.


#8

okay one more question here..
in associative arrays you write echo $array["key"] to show the value. how do you do the same in javascript?

consider this code..

var friends={
    bill:{
        "firstName":"Bill",
        "lastName":"Demott",
        "number":0011,
         },
    steve:
  {
  "firstName":"Steve",
  "lastName":"Austin",
  "number":0012,
},
    };

how do i print lastName property of bill? tested console.log(bill.lastName); and it doesn't work here.


#9

bill.lastName is undefined, but, friends.bill.lastName is defined. We can iterate over the secondary objects (nested as values in primary properties) with a nested loop construct:

for (var key in friends){
    console.log(key);
    for (var k in friends[key]){
        console.log(k + ": " + friends[key][k]);
    }
}

We won't likely see this very much, but here it is:

friends['bill']['firstName']

#10

@mtf
interesting, this is like accessing a specific value of a multidimensional array

thank you very much for all the help.