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

160
Vistas
Get name of an object that is empty

I would like to get variable name of an object which is empty. How can i get this?

My code

var array = [foo, bar, baz]
array.map((el)=>{
 if(Object.keys(el).length === 0) {
  //give me name of var from an array which is empty
 }
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There's no way to get to a variable name from some arbitrary value. But you can supply meta information yourself. For example:

const foo = {a: 42};
const bar = {};
const baz = {b: 451};

// Use an object, instead of an array. With the following syntax
// the variable *creates* a property of the same name.
const configs = {foo, bar, baz};

// find *empty* elements:
const emptyConfigs = Object.entries(configs).reduce((acc, [k, cfg]) => {
  return Object.keys(cfg).length === 0
    ? [...acc, k]
    : acc;
}, []);

console.log(emptyConfigs);

Ref: Object initializer


Alternatively include the information with each array entry. Which has the added benefit that the configs can/could be inlined (and thus don't have a name). E.g.:

const foo = {a: 42};
const bar = {};
const baz = {b: 451};

const configs = [
  {key: 'foo', cfg: foo},
  {key: 'bar', cfg: bar},
  {key: 'baz', cfg: baz},
  {key: 'unnamed', cfg: {}}, // inlined config
];

const emptyConfigs = configs.reduce((acc, {key, cfg}) => {
  return Object.keys(cfg).length === 0
    ? [...acc, key]
    : acc;
}, []);

console.log(emptyConfigs);

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