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

157
Views
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 answers
Answer question

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 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!