access object property javascript variable code example

Example 1: javascript dynamically access object property

var person ={"first_name":"Billy","last_name":"Riley"};
var property_name="first_name";
alert(person[property_name]); //Dynamically access object property with bracket notation

Example 2: access object attribute javascript

const json = {
    nome: "nome",
    sobrenome: "sobrenome",
    idade: "idade",
    endereco: "endereco",
    empresa: "empresa",
    telefones: [
        {
            "residencial": "residencial"
        },
        {
            "celular": "celular"
        }
    ]
}

console.log(Object.keys(json))
// ["nome", "sobrenome", "idade", "endereco", "empresa", "telefones"]