Objects in Javascript - Get

Asked

Viewed 38 times

1

Hello, I just started learning Javascript and I was wondering how to get the Property Artist of the second object of myMusic in the console.log.

var myMusic = [
    {
        "artist": "Billy Joel",
        "title": "Piano Man",
        "release_year": 1973,
        "formats": [
            "CD",
            "8T",
            "LP"
        ],
        "gold": true
    },
    // Add another record
    {
        "artist": "Billie Eilish",
        "title": "Don't Smile At Me",
        "release_year": 2017,
        "formats": [
            "CD",
            "LP",
            "Streaming Services"
        ],
        "gold": false
    }
]
console.log(myMusic);

Thank you very much.

1 answer

2

Its variable myMusic is a array, of two positions, each position contains one object.


To access the position of an array, we use square brackets and between them the desired index, so would be as follows to take the data of the second object:

console.log(myMusic[1]);

That’s because the arrays start at zero in Javascript.


To access the properties of an object, we use the ., That’s right the end point, so to take the property artist, we have to access the array in the second position and then use the point to access the property:

console.log(myMusic[1].artist);

In addition to using the ., it is also possible to use square brackets and the desired property name among the square brackets:

console.log(myMusic[1]["artist"]);

var myMusic = [
    {
        "artist": "Billy Joel",
        "title": "Piano Man",
        "release_year": 1973,
        "formats": [
            "CD",
            "8T",
            "LP"
        ],
        "gold": true
    },
    // Add another record
    {
        "artist": "Billie Eilish",
        "title": "Don't Smile At Me",
        "release_year": 2017,
        "formats": [
            "CD",
            "LP",
            "Streaming Services"
        ],
        "gold": false
    }
];

console.log(myMusic);

//Pegar as informações do primeiro artista
console.log(myMusic[0]);

//Pegar o nome do primeiro artista
console.log("Nome:", myMusic[0].artist);

//Pegar as informações do segundo artista
console.log(myMusic[1]);

//Pegar o nome do segundo artista
console.log("Nome:", myMusic[1].artist);


We can use loops and iterate on the object in order to take all the data:

var myMusic = [
    {
        "artist": "Billy Joel",
        "title": "Piano Man",
        "release_year": 1973,
        "formats": [
            "CD",
            "8T",
            "LP"
        ],
        "gold": true
    },
    // Add another record
    {
        "artist": "Billie Eilish",
        "title": "Don't Smile At Me",
        "release_year": 2017,
        "formats": [
            "CD",
            "LP",
            "Streaming Services"
        ],
        "gold": false
    }
];

//for no array
for ( const artista of myMusic ) {
  //for nas propriedades do objeto
  for ( const propriedade in artista ) {
    console.log(propriedade, artista[propriedade]);
  }
}


You can also access the array with a simple for, taking the size of the array and accessing the property you want so much with ., when using brackets and the property name:

var myMusic = [
    {
        "artist": "Billy Joel",
        "title": "Piano Man",
        "release_year": 1973,
        "formats": [
            "CD",
            "8T",
            "LP"
        ],
        "gold": true
    },
    // Add another record
    {
        "artist": "Billie Eilish",
        "title": "Don't Smile At Me",
        "release_year": 2017,
        "formats": [
            "CD",
            "LP",
            "Streaming Services"
        ],
        "gold": false
    }
];

for ( let i = 0; i < myMusic.length; i++ ){
  console.log( myMusic[i].artist );
  //OU
  console.log( myMusic[i]["artist"] );
}


Documentations:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Trabalhando_com_Objetos

https://developer.mozilla.org/en-US/docs/Aprender/JavaScript/Objetos/B%C3%A1sico

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

Browser other questions tagged

You are not signed in. Login or sign up in order to post.