Manipulating Complex Objects-help

Manipulating Complex Objects-help
0

#1

What’s wrong here.Manipulating Complex Object?

Your code so far

var myMusic = [
  {
    "artist": "Billy Joel",
    "title": "Piano Man",
    "release_year": 1973,
    "formats": [ 
      "CS", 
      "8T", 
      "LP" ],
    "gold": true
  }
  // Add record here
  var myMusic = [
  {
    "artist" : "Daddy Yankee",
    "title" : "Barrio Fino",
    "release_year" : 2004,
    "formats" : [ 
      "CS", 
      "8T", 
      "LP" 
]
  }
];

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299.

Link to the challenge:


#2

myMusic should be an array of elements whose elements are objects. How do we separate elements in an array? You should not be declaring myMusic array a second time. You just want to add onto the original one using a separator as I mentioned above.


#3

still in stuck
var otherMusic = [
{
“artist” : “Linkin Park”,
“title” : “Meteora”,
“release_year” : 2004,
“formats” : [
“CS”,
“8T”,
“LP”, ]

}
];


#4

You should not be creating any additional arrays. myMusic is already declared with one element (an object containing album information). You just need to add an additional object (a different album) to it.

Above, you created another called otherMusic and put one object in it. Put that object you created for otherMusic as the 2nd object of myMusic. Just remember, elements in an array are separated by a certain character. If you can not remember what the special character is, then review the following challenge.


#5

Hi zsinyi2,

It is possible create a new array, but you need add a index for new album . Remember that the myAlbum “Billy Joel” is on index [0]. So we need add a second array for the first array, creating a multidimensional Array. There are two ways to get it. See lessons Access Multi-Dimensional Arrays With Indexes and Manipulate Arrays With push().

//The fisrt way to achieve it is using index. Create a new array, in this case below I call it var myNewAlbum and after apply that new array as a new index for array myAlbum.

var myNewAlbum =
{
“artist”: “Skank”,
“title”: “Jack Tequila”,
“release_year”: 1994,
“formats”: [
“CS”, “8T”, “LP”]
};
// Applying index:
myMusic[1] = myNewAlbum;

//The second way to achieve it is keeping the var my NewAlbum and to replacing the last line from code above using .push().

var myNewAlbum =
{
“artist”: “Skank”,
“title”: “Jack Tequila”,
“release_year”: 1994,
“formats”: [
“CS”, “8T”, “LP”]
};

//Now, just replace the last line using .push() to add the new array for myMusic like that:
myMusic.push(myNewAlbum);

Test both yourself. :wink:

//Data structure is pretty important concept in computing. Check other explanation sources about Data Structure, like wikipedia. I reaaly hope that be a help.


#6

@delsonscoelho - Your code does not work, because you only need to add an object to the 2nd element of myMusic and not an array with an object as it’s only element. Why? Because myMusic is already an array and you are only supposed to add an object.


#7

Yes randelldawson. I checked again and the code was wrong due sintaxe. I was using brackets [ ] , to start a new Array, but It was wrong , because bracket refer to external and first array. The new array need start and finish with braces { }.

I put a new answer using two solutions and both work well:

//below using .push();
var myNewAlbum =
{
“artist”: “Skank”,
“title”: “Jack Tequila”,
“release_year”: 1994,
“formats”: [
“CS”, “8T”, “LP”]
};

myMusic.push(myNewAlbum);

//Below using a new index, just replacing the last line above:
myMusic[1] = myNewAlbum;