Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

204
Visualizações
What meaning the dash in array when iterating an array in for of loop with entries iterator?

If I normally use for of loop and use iterator as entries that situation look like this:

var a = ['a', 'b', 'c'];
var iterator = a.entries();

for (let e of iterator) {
  console.log(e);
}
// [0, 'a']
// [1, 'b']
// [2, 'c']

iterator: will be the entire array that contain all elements key/value pair. Key will be the index.

e: will be an element in the array

BUT what is this??????

let text = "A A A";
let splitwords = text.split(" ");
let words = [];
for (const [, item] of splitwords.entries()) {
  words.push(item.split(""));
}

console.log(`This is the words: ${words}`);

what meaning the [, item] part???

and why should i use this pattern?

text.split("") do exactly same thing or not?

(Otherwise I try solve an text animation problem and this inherit from that code:

framer motion text animation )

Thank you

PS: I know this is an array destructing my main question that why????

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This is a destructuring assignment.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Array.prototype.entries() returns an array of 2 elements for the index and the item itself for each array item.

So in this expression the index is assign to nothing (because there is no variable declared before the comma) and the item is assigned to item.

Example

const ar = [1,2]
const [,value2]=ar
console.log(value2)
const [value1,]=ar
console.log(value1)

const [v1, v2] = ar
console.log(v1, v2)

For the item.split("") it just seems useless, the result would be the same in the present case with just words.push(item) since each word is just one letter... If the words were more than one letter this would just store each letter separately in the words array. Which could maybe be called letters I guess...

Edit: for the "why use this pattern" question. In the present case, again it just seems useless. The index is not used so calling entries just don't seems relevant.

about 4 years ago · Juan Pablo Isaza Relatório

0

It's just a way to skip destructuring the first element in the array. You are basically saying that you are not interested in the first array item. Since the entries() method returns a new Array Iterator object that contains the key/value pairs for each index in the array.

const foo = ['a', 'b', 'c'];

for (const [, item] of foo.entries()) {
  // item would be 'a', 'b', 'c'
}

for (const [index, item] of foo.entries()) {
  // item would be 'a', 'b', 'c'
  // index would be 0, 1, 2
}
about 4 years ago · Juan Pablo Isaza Relatório

0

for (const [, item] of splitwords.entries()) {
  words.push(item.split(""));
}

[, item] is called array destructing, and the reason for that is because of entries in splitwords.entries(). The result of that array is like this [0, "a"],[1, "b"] (the first item is an index, and the second item is value), but in this case, they don't use an index, so the declaration is like [,item] (If they want to use it, the code can be [index, item]).

Without using an index, they can implement this way

for (const item of splitwords) { //remove entries and array destructing
  words.push(item.split(""));
}

and why should I use this pattern?

Well, I think this is just their code style to use for of for collecting both index and value together. It depends on you need an index or not.

words.push(item.split("")) is different from the above case, they try to make the entire word "Hello" into characters ["H","e","l","l","o"] for the animation, so the final result of words can be

[
  ["F","r","a","m","e"],
  ["M","o","t","i","o","n"],
  ...
]
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda