Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

326
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda