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

146
Views
Include unavailable objects in filtering

I am new to JS. I want to include all the unavailable objects of Value while filtering. In the code below, I am applying the condition Value >= 4 && Value < 6. I want (Value >= 4 && Value < 6) || unavailable values of Value`

var Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]

result = Annual.filter(function(v) { return v.Value >= 4 && v.Value < 6; })

console.log(result);

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Add a !v.Value condition to your boolean expression

var Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]

result = Annual.filter(function(v) { return (v.Value >= 4 && v.Value < 6) || !v.Value; })

console.log(result);

Edit:

Like said in a comment below, in a case that you would not to consider that any falsy value is invalid (like zero, empty string, etc), you might prefer using the Object.prototype.hasOwnProperty method.

const Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]

const result = Annual.filter(function(v) { 
    return (v.Value >= 4 && v.Value < 6) || !v.hasOwnProperty("Value"); 
})

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

typeof check if value undefined make the job too.

const Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]

result = Annual.filter((v) => v.Value >= 4 && v.Value < 6 || typeof v.Value === "undefined");

console.log('res',result);

about 4 years ago · Juan Pablo Isaza Report

0

use hasOwnProperty method to check if the object has a property called Value and negate it.

var Annual = [{"Date":1998,"Value":6.5,"GDPAnn":9062800},{"Date":1999,"GDPAnn":9631200},{"Date":2000,"Value":4.1,"GDPAnn":10251000},{"Date":2001,"GDPAnn":10581900}]

result = Annual.filter(function(v) { return (v.Value >= 4 && v.Value < 6) || !v.hasOwnProperty('Value') })

console.log(result);

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!