Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!