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

333
Views
¿Cómo puedo obtener objetos anidados que no tengan valores vacíos?

He escrito esta función

 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]; })); };

su objeto de retorno con par de valores clave

 { exampleKey: "someNotEmptyUrl" }

pero quiero que devuelva este tipo de objeto

 { action: exampleKey url: "someNotEmptyUrl" }

¿tal vez solía escribir reducir en lugar de mapear?

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

0

Puede intentar usar Object.entries y luego extender el operador para construir un nuevo objeto:

 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

En caso de que tenga más de un objeto con un valor de URL no vacío, cree una nueva matriz e push nuevos objetos en ella.

 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!