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

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

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 Denunciar

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 Denunciar

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