Manipulating Complex Objects - adding new records

Tell us what’s happening:
If we can add a new record to an array using bracket notation?

Your code so far

var myMusic = [
  {
    "artist": "Billy Joel",
    "title": "Piano Man",
    "release_year": 1973,
    "formats": [
      "CD",
      "8T",
      "LP"
    ],
    "gold": true
  }
  // Add record here
];
myMusic[2] = [
    "artist": "Coldplay"
    "title": "The Scientist"
    "release_year": 2002
    "formats": ["CD", "MP3"]
]

Challenge: Manipulating Complex Objects

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/manipulating-complex-objects

Yes you can, I don’t know if the challenge let you do that, but that would be correct use of bracket notation, if you used the correct index (arrays are 0-indexed, the first element is at index 0, the second at index 1 and so on… so you would need to write myMusic[1] = ...), and second most important thing, if you used correct syntax

this is not an object, this is a bunch of nonsense:

[
    "artist": "Coldplay"
    "title": "The Scientist"
    "release_year": 2002
    "formats": ["CD", "MP3"]
]

an object is:

  • surrounded by graph brackets {}
  • have pairs of property names and property values propName: "propValue"
  • each pair is separated by commas
{propName1: "propValue1", propName2: "propValue2"}
1 Like

I’ve added commas and everything’s good.
I have another question:

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

];

Here you have array built like this:

var arr = [0,  2,  3 [ 4, 5, 6], 7]

Can you return every of these values inside array using bracket notation?
I tried and I can’t

first, you need to put a comma between each element. After the 3 there is a missing comma
once fixed that, it is possible

var arr = [0, 2, 3, [4, 5, 6], 7];
arr[0] // 0
arr[1] // 2
arr[2] // 3
arr[3] // [4, 5, 6]
arr[3][0] // 4
arr[3][1] // 5
arr[3][2] // 6
arr[4] // 7

and if you mean for the array inside the object…

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

];

myMusic[0]["formats"] // ["CD", "8T", "LP"]
myMusic[0]["formats"][0] // "CD"
// and so on

My mistake. It’s of course an array inside object inside an array. Thanks for explanation.