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

130
Visualizações
Reduce method returns

Why does the second assignment need return and the first returns variable total automatically?

const total = inventors.reduce(((total, inventor) => total += inventor.passed - inventor.born), 0);
const transportation = data.reduce(((prevVal, currentVal) => {
      if (!prevVal[currentVal]) {
        prevVal[currentVal] = 0;
      }
      prevVal[currentVal]++;
      return prevVal;
    }), {})

If return prevVal is omitted I get Uncaught TypeError: Cannot read properties of undefined

What is the difference in how reduce() acts with values. Is it effected by ternary operation? If so then why this returns 0?

const transportation = data.reduce(((prevVal, currentVal) => 
(!prevVal[currentVal]) ? prevVal[currentVal] = 0 : prevVal[currentVal]++), {})
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When using curly braces with an arrow function, the return is not implicit, so you have to use return

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