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

132
Visualizações
In reduce, previousValue becomes undefined when I add conditional lines inside that performs .some and inside it performs .includes

The prevValue becomes undefined, but when I remove the conditional lines (170) above undefined is gone.

But when I add the prevValue as condition in the line 173, it won't perform the line below anymore.

What I wanted is when a condition is meet, then I can push an item inside prevValue.

I have the whole example that we can examine here in codesandbox https://codesandbox.io/s/goofy-feather-t79kb6?file=/src/index.js

Sorry, I spent hours on this simple one yet time consuming for me. Your help is highly appreciated. Thank you.

enter image description here

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

0

The returned value of the callback function becomes the new accumulated value, with every iteration.

So, if we are not updating it, we have to return the old value itself.

arr.reduce((acc,item)=>{
  if(condition) return acc.concat(item);
  //else
  return acc; //-->should not miss this.
})

I think this eslint rule https://eslint.org/docs/rules/array-callback-return will help catch this type of issues early.

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