1

i want to try to display my object item in console with console.log but console shows it as undefined. Can you help me?

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

console.log(myMusic.artist);

I am tried that, and i was expected to see Billy Joel in console but it wrote undefined in console

1 Answer 1

2

This is a simple mistake, check comments in the codeblock, if i can help further, please comment

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

console.log(myMusic.artist); // undefined because myMusic is an array
console.log(myMusic[0].artist); // "Billy Joel"
console.log(myMusic[1].artist); // "Beau Carnes"
console.log(myMusic[0]) /* 
{
    "artist": "Billy Joel",
    "title": "Piano Man",
    "release_year": 1973,
    "formats": [
      "CD",
      "8T",
      "LP"
    ],
    "gold": true
  } */
Sign up to request clarification or add additional context in comments.

5 Comments

Hello, thanks for the answer. But in the lesson video, it says it is an object. Actually is it not object?
It is an array, but the items in the array are objects :)
You can fetch the items in the array with bracket notations like so: array[0] (First item), array[1] (second item) and so on In objects you can get the values by dot notation or brackets that takes a string object.property // 'value' object['property'] // 'value'
@Kjetil please don't order users to accept your answer. That's not how this site works.
I'm sorry, I meant it as a request, but I see how it can be taken as an order Thanks for letting me know, @Andy

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.