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

467
Visualizações
Array.prototype.map() expects a value to be returned at the end of arrow function

I have this array.map() arrow function and have the following error: "Array.prototype.map() expects a value to be returned at the end of arrow function array-callback-return"

Here is my code:

          {productList.map((val) => {
            const category = val.CategoryName;
            // const quantity = val.ProductQuantity
            if (category === "MotorcycleParts") {
              if (val.ProductQuantity !== 0) {
                return (
                  <div>
                    <div>
                      <div class="buyNowBtnDiv">
                        {localStorage.getItem("username") === "" ||
                        localStorage.length === 0 ? (
                          <div id="buyAddBtn">
                          </div>
                        ) : (
                          <div id="buyAddBtn">
                          </div>
                        )}
                      </div>
                    </div>
                  </div>
                );
              }
            }
          })}

I have read about the error and I see it has something to do with requiring a returning value, but I do have this?

A code example with a fix would help, I think it would be something simple.

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

0

You could just return null when the listed conditions are not matched:

          {productList.map((val) => {
        const category = val.CategoryName;
        // const quantity = val.ProductQuantity
        if (category === "MotorcycleParts") {
          if (val.ProductQuantity !== 0) {
            return (
              <div>
                <div>
                  <div class="buyNowBtnDiv">
                    {localStorage.getItem("username") === "" ||
                    localStorage.length === 0 ? (
                      <div id="buyAddBtn">
                      </div>
                    ) : (
                      <div id="buyAddBtn">
                      </div>
                    )}
                  </div>
                </div>
              </div>
            );
          }
         return null;
        }
      })}
about 4 years ago · Juan Pablo Isaza Relatório

0

map has to return a value for each item.

If you want to do some filtering, you should consider using Array.prototype.filter() before, so you can remove the ifs in your map.

The code is also way more easy to read.

      {productList.filter(val => val.CategoryName === "MotorcycleParts" && val.ProductQuantity !== 0)
                  .map((val) => {
            return (
              <div>
                <div>
                  <div class="buyNowBtnDiv">
                    {localStorage.getItem("username") === "" ||
                    localStorage.length === 0 ? (
                      <div id="buyAddBtn">
                      </div>
                    ) : (
                      <div id="buyAddBtn">
                      </div>
                    )}
                  </div>
                </div>
              </div>
            );
      })}
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