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

153
Visualizações
Using forEach to chain methods

One of the advantages I've heard for the use of forEach vs a for/of on an iterable is that you can chain methods together.

As a basic example of the two I have:

let arr = [1,2,3];
arr.forEach((elem, idx) => {
    console.log(elem);
})
for (let elem of arr) {
    console.log(elem);
};

What would be an example of chaining together multiple forEach's?

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

0

forEach returns undefined. You can't call a method on undefined.

The chaining you might have heard about is when forEach is at the end of some processing which returns an array, like after array methods like map,reduce etc:

let arr = [1,2,3];

arr.map(x => x*2).forEach((elem, idx) => {
    console.log(elem);
})

about 4 years ago · Juan Pablo Isaza Relatório

0

forEach always returns undefined. forEach is not chain-able like map, reduce or filter.

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