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

325
Views
How can I get nested objects, which have not empty values?

I have write this function

export const func = (object) => {
    return Object.fromEntries(Object.entries(object).filter(([, {
        url,
        value
    }]) => {
        return (url && url !== '') || (value && value !== '');
    }).map(([key, { url, value }]) => {
        return [key, url ? url : value];
    }));
};

its return Object with key value pair

{
  exampleKey: "someNotEmptyUrl"
}

but i want it to return this type of object

{
  action: exampleKey
  url: "someNotEmptyUrl"
}

maybe i used to be write reduce instead of map ?

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

0

You can try using Object.entries and then spread operator to build a new object:

const func = (object) => {
    let [action,v] = Object.entries(object).find(([k,v]) => !!v.url);
    return ({ action, ...v });
};

const obj = { "action1": {"url": ""}, "action2": {"url": ""}, "action3": {"url": ""}, "action4": {"url": "notEmpty"}, "action5": {"value": ""} };

console.log(func(obj));

about 4 years ago · Juan Pablo Isaza Report

0

In case you have more than one object with a non-empty URL value create a new array and push new objects into it.

const obj={action1:{url:""},action2:{url:"notEmpty2"},action3:{url:""},action4:{url:"notEmpty4"}};

const arr = [];

for (const [action, value] of Object.entries(obj)) {
  const { url } = value;
  if (url) arr.push({ action, url });
}

console.log(arr);

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!