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

143
Visualizações
Javascript Array.Filter method not working

im do not understand why this filter call is not working? it should be returning the one item that has the 100 dollar invoice but it is instead sending an empty set. What am i doing wrong?

const express = require('express');
const app = express();

app.listen(3000);
app.use(express.static('public'));
app.set('view engine', 'ejs');

const invoices = [
    {
        INVOICE_AMOUNT: 50
    },
    {
        INVOICE_AMOUNT: 100
    },
    {
        INVOICE_AMOUNT: 200
    }
];

app.get('/', (req, res) => {
    res.send(invoices.filter(x=>{x.INVOICE_AMOUNT==100}));
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The filter method needs a return value that is either truthy or falsy. If you return a truthy value, it keep the element, otherwise it filters it out.

Since you have no return statement nor you have an array function without parens, it always returns undefined and filters out all elements. Solutions are:

invoices.filter(x => { return x.INVOICE_AMOUNT == 100 })
// or
invoices.filter(x => x.INVOICE_AMOUNT == 100)

In the first case, there is an explicit return statement, in the second case, omitting the parens uses an implicit return statement.

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