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

173
Views
Return only the values that are not empty string or empty arrays from an object

Until now I have the following function:

export const useClearEmptyAttributes = (
  obj: { [s: string]: unknown } | ArrayLike<unknown>,
) => {
  return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v));
};

enter image description here

It eliminates an empty field "phone number" which was empty, but not socialLinks (which is an array).

How can I modify my function to eliminate empty arrays too, not just empty strings?

Thank you!

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

0

since you are targeting empty strings and empty arrays and both have length property you can check for the length if it is bigger than 0:

export const useClearEmptyAttributes = (
  obj: { [s: string]: unknown } | ArrayLike<unknown>,
) => {
  return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v.length > 0));
};
about 4 years ago · Juan Pablo Isaza Report

0

This would work if you want to add checking empty arrays plus your current check:

export const useClearEmptyAttributes = (
  obj: { [s: string]: unknown } | ArrayLike<unknown>,
) => {
  return Object.fromEntries(Object.entries(obj).filter(([_, v]) => Array.isArray(v)?v.length:v));
};

But if you want to check also for empty objects, etc it can be further modified.

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!