Doubt to remove JavaScript attributes

2

Given the following object:

var pessoa = {
    nome: 'Fernando',
    idade: 15,
}

I would like to know what is the most RIGHT way (if there is a more correct way) to remove one of these attributes.

    
asked by anonymous 22.12.2016 / 19:53

1 answer

5

You can use delete operator :

var pessoa = {
    nome: 'Fernando',
    idade: 15,
};

// verifica se nome existe
if (pessoa.nome)
 delete pessoa.nome;

console.log(pessoa);
    
22.12.2016 / 19:56