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

161
Views
Why does the Logical Or not work in my function

How can i modify the blow sample code to not only check for empty Keys but also null and undefined.
I tried

(obj[key] !== '' || obj[key] !== null || (obj[key] !== undefined)  

but that broke it and didnt work at all so if i use either condition it will work but not when all together. So i am wondering how i can combine it all 3 conditions in this code.

const removeEmpty = (obj) => {
  let newObj = {};
  Object.keys(obj).forEach((key) => {
    if      (obj[key] === Object(obj[key])) newObj[key] = removeEmpty(obj[key]);
    else if (obj[key] !== '')               newObj[key] = obj[key];
  });
  return newObj;
}

var obj = {
  Sale: {
    homeownerExemption: '',
    lastContractDate: '',
    lastSaleBookNumber: undefined,
    lastSaleDate: null,
    saleType: 'Full Sale',
    salesPrice: '785000',
    salesPriceCode: 'Sales Price Will Be Computed',
    seller1FName: '',
    seller1IdCode: '',
    seller1FirstName: 'Steve',
    seller1LastName: 'Miller',
    seller2FirstName: '',
    seller2LastName: '',
    transferType: 'Grant Deed',
    lastTransactionRecordingDate: '7/8/2021'
  },
  contact: [{
    name: 'Tom',
    age: '',
    sex: 'male'
  }]
};


const removeEmpty = (obj) => {
  let newObj = {};
  Object.keys(obj).forEach((key) => {
    if (obj[key] === Object(obj[key])) newObj[key] = removeEmpty(obj[key]);
    else if (!(obj[key] === "" || obj[key] === null || obj[key] === undefined)) newObj[key] = obj[key];
  });
  return newObj;
};

let test = removeEmpty(obj)

console.log(test)

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

0

If obj[key] === "", then it's not equal to null or undefined. So the second and third parts of your condition pass.

Try

if (!(obj[key] === "" || obj[key] === null || obj[key] === undefined))
about 4 years ago · Juan Pablo Isaza Report

0

You need to parenthesize the condition properly. Don't be cheap out on the parens, they're free! A reasonable way could be like this:

((obj[key] !== '') || (obj[key] !== null) || (obj[key] !== undefined))

Your condition has an extra ( after second ||.

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!