1
I couldn’t understand the following code:
//Esse array vai modificar o DOM com o seu conteúdo
const elementos = [
{ tag: 'p', texto: 'Frase 1' }, // indice 0
{ tag: 'div', texto: 'Frase 2' }, // indice 1
{ tag: 'section', texto: 'Frase 3' }, // indice 2
{ tag: 'footer', texto: 'Frase 4' } // indice 3
];
const container = document.querySelector('.container');
const div = document.createElement('div');
for (let i = 0; i < elementos.length; i++) {
const { tag, texto } = elementos[i];
const elemento = document.createElement(tag);
const noTexto = document.createTextNode(texto);
elemento.appendChild(noTexto);
div.appendChild(elemento);
console.log(elementos[i]);
}
container.appendChild(div);
The code is working normally, in case my question is: Why when assigning, is it not necessary to put the brackets? Ex: [{tag, texto}] = elementos[i];
Why would I need it? This is a syntax designed specifically for de-structuring, it could be anything. But think about it, the clasp is already de-referencing on the other side ;)
– bfavaretto
Because when I saw assignment via unstructuring, to pick up an Object inside the array, I saw that I needed to use the brackets. Ex: const exem = [ 'bla', 'bla', {name: 'tal'}]; const [, {name}] = exem;
– MrVladzzer
In the question code you are not structuring the object from within the array, you already "removed" the array when accessing element[i]
– bfavaretto
I get it. Thanks, man, you removed a flea from behind my ear! I haven’t moved on in my studies, because I didn’t understand that, now I’ll continue!
– MrVladzzer