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

176
Views
How to include data to object if element is not falsy?

I have the map:

map(([reportProperties, reportObjectsProperties, textProperties, visibleText]) => {
                return { reportObjectsProperties, reportProperties, textProperties, visibleText };
            }),

I try to check if all parameters are not falsy include them to result object like this:

map(([reportProperties, reportObjectsProperties, textProperties, visibleText]) => {
                if(visibleText)
                   return { reportObjectsProperties, reportProperties, textProperties, visibleText };
                if(reportObjectsProperties && reportObjectsProperties.length)
                   return { reportObjectsProperties, reportProperties, textProperties, visibleText };

....

            }),

How to make this more elegant?

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

0

Assuming your object is an array, you can check if all values exists using every method. And you don't need map there because you don't do a transformation on your object.

instead of

.map(arr => {
 ... return arr;
});

do

.filter(row => row.every(cell => typeof cell !== undefined && cell !== null));
about 4 years ago · Juan Pablo Isaza Report

0

My suggestion is to look into Array.prototype.filter() method paired with Object.entries to filter out the falsy values. And pair it with a reduce function to reconstruct the new object

I would take this into the following direction:

const arr = [{a: true, b: true, c: false}, {a: false, b: true}]

const excludeKeys = ["c"]


let result = arr.map(obj => {
  return Object.entries(obj).filter(([key, value]) => !excludeKeys.includes(key) && value)
    .reduce((prev, [key, value]) => ({...prev, [key]: value}), {})
})

This provides a functional and pretty generic interface for filtering out the falsy / excluded object key / values.

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!