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

151
Views
How to return array of objects from multidimensional object?

I have object:

const salary = {
   netto: {
      min: '700.',
      max: '',
   },
   brutto: {
      min: '',
      max: '',
   },
}

I want to iterate through object and return array of objects that would look like [{name: 'netto_min', value: '700'},...etc] skipping keys with empty value.

I tried:

const result = Object.keys(salary).map(type => 
   Object.keys(salary[type]).map(entry => {
      if(salary[type][entry] !== ''){
         return {name: `${type}_${entry}`, value: parseInt(salary[type][entry]).toString()}
      }
   })
)

It got me: [[{name: 'netto_min', value: '700'}, undefined],[undefined, undefined]] How can I get [{name: 'netto_min', value: '700'}] instead?

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

0

const salary = {
  netto: {
    min: "700.",
    max: ""
  },
  brutto: {
    min: "",
    max: ""
  }
};
let result = Object.keys(salary).map(k => {
  return Object.keys(salary[k]).map(r => ({
     name: `${k}_${r}`,
     value: parseInt(salary[k][r])
    }))
}).flat().filter( b => !isNaN(b.value))

Returns

[ { name: 'netto_min', value: 700 } ]

I'm assuming that the 700. was a typo on 700, easily stripped out

about 4 years ago · Juan Pablo Isaza Report

0

You could do this:

const salary = {
  netto: {
    min: '700.',
    max: '',
  },
  brutto: {
    min: '',
    max: '',
  },
}

let result = []

for (type in salary) {
  for (entry in salary[type]) {
    let obj = {}
    let value = salary[type][entry]

    if (value) {
      obj.name = `${type}_${entry}`
      obj.value = parseInt(value).toString()

      result.push(obj)
    }
  }
}

console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

Get the keys, then filter that array by if that key is non-empty. then iterate through, and push it to the array and return it.

const salary = {
  netto: {
    min: '700.',
    max: '',
  },
  brutto: {
    min: '',
    max: '',
  },
};

var non_empty_salaries = Object.keys(salary).map(name => {
  var keys = Object.keys(salary[name]);
  var non_empty = keys.filter(key => salary[name][key] != '');
  var non_emptys = [];
  for (let key of non_empty) {
    non_emptys.push({
      name: `${name}_${key}`,
      value: salary[name][key]
    });
  }
  return non_emptys;
});
console.log(non_empty_salaries);

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!