Concept behind the lesson: Manipulating Complex Objects

Tell us what’s happening:
I have passed the test but I have difficulties in understanding the use of in the lesson.

I have found an explanation of the whole code below:

According to the explanation, the use of on the first line is to open array of objects, which according to the example, means more than one music album.

var myPets = [              // open array of objects

Does it means that, if there is only one album here, like below:


var myMusic = [
{
  "artist": "Billy Joel",
  "title": "Piano Man",
  "release_year": 1973,
  "formats": [
    "CD",
    "8T",
    "LP"
  ],
  "gold": true
},
];

In the above example, will the first still be necessary? Or I only need to type the code as below?

var ourMusic = 
  {
    "artist": "Daft Punk",
    "title": "Homework",
    "release_year": 1997,
    "formats": [ 
      "CD", 
      "Cassette", 
      "LP"
    ],
    "gold": true
  }
;

My second question is about the “formats”
is there any differences between
1.

    "formats": [ 
      "CD", 
      "Cassette", 
      "LP"
    ],

and
2.

    "formats": 
      "CD", 
      "Cassette", 
      "LP"
    ,

Thank you for whoever helps!

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15

Challenge: Manipulating Complex Objects

Link to the challenge:

Question 1

  • If you are sure that there will not be another music in your music library, you may think you don’t need the array.
  • But you cannot predict that future.
  • So if it in an array, when ever you want another music to be in your music library, you can just push it inside your myMusic array.

Question 2

  • The following is an array containing multiple strings
    "formats": [ 
      "CD", 
      "Cassette", 
      "LP"
    ],
  • But the following is not a valid JavaScript primitive or object
    "formats": 
      "CD", 
      "Cassette", 
      "LP"
    ,
  • You need a thing to hold the formats. In the first case the array holds it and then set is as the value to formats. But in the second case there is nothing to hold it together.
1 Like

Regarding the specific challenge, myMusic is an array( [ ] ) of music albums. Each album data is stored inside an object( { } ). Turning my myMusic into a single album object, would defy the concept of the array/list. Arrays can have zero or more elements, so having a single element(object) in the case is not to worry.
Like user before me stated, you cant list values simply with commas, under the current syntax. In the case "formats": [ "CD", "Cassette", "LP" ] , formats is a key(property) of an object. Its value is an array of strings(representing formats). The value assigned to an object key can be any valid JS type, a boolean, string, array, another object, function, a number etc. There is no value type represented as "CD", "Cassette","LP" . Assigning such thing to an object key would be equivalent of trying to assign it to a regular variable. Imagine the following impossible syntax:

var format= "CD", "Cassette","LP"

You cant assign few values to one variable, the same goes when it comes to an object key :slight_smile:

1 Like

Referring to your explanation to my 1st question, is it right for me to say, that coding practice (using array when my music library don’t have another music at the moment I code) is a sort of best practice for further development of the code, and for understanding of other programmer?

I apologise if my question sounds weird, I am trying to learn both the logic behind the code and how programers streamline their work flow as I am not working in the industry.

Once again, thanks a lot!

if the purpose of a variable is to store a list of some stuff, objects representing music albums in the current example, its best practice to initiate it as an array, even if it does not include any data initially. In general, its best to initiate your data in the shape and form its meant to represent.
Very often with coding you want to pre-defined functions code etc to apply to your data, like in the curent example, you could make a function to add albums to the list, or modify certain properties in the list items. If your data were in fluid state(it start as an object, becomes and array once it contains many objects, turn back into a single object if other albums are being removed), this would involve additional complications in writing your code and can lead to errors

1 Like

Got it! I will have this in mind when I code. Thx!

It’s a list of items, an array. A list of items can have no items or one just item in it, but it’s still a list of items. If you don’t put the object in an array, then it’s not the same thing

With this, myMusic represents a collection of albums, and a collection of albums is not the same thing as an album. If it was physical, it could be a shelf in your house where you put the physical records. The shelf, though, is not the same type of thing as a record

2 Likes