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

212
Vistas
How to iterate through nested Objects and combine key and value?
const products = {
  value: {
    phone: [],
    lamp: [],
    car: ["bmw", "toyota", "audi"]
  }
};

In the given object I want to iterate and get keys.
Note that values are array of strings.

I'd like to get all keys and if values are not empty arrays I want to combine with keys and return an array with this structure

   [
    phone,
    lamp,
    car-bmw,
    car-toyota,
    car-audi
  ]

Here is what I did so far, which is not what I want

const products = {
  value: {
    phone: [],
    lamp: [],
    car: ["bmw", "toyota", "audi"]
  }
};

const mappedProducts = Object.entries(products.value).map(([value, items]) => {
  //console.log("value :", value, "items :", items)
  if (items.length > 0) {
    return {
      car: items.map((c) => c)
    };
  }
});

console.log(mappedProducts);

Any help will be appreciated.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

I modified what you did a bit, you can you flatMap

const products = {
  value: {
    phone: [],
    lamp: [],
    car: ["bmw", "toyota", "audi"]
  }
};

const mappedProducts = Object.entries(products.value).flatMap(([value, items]) => {
  //console.log("value :", value, "items :", items)
  if (items.length > 0) {
    return items.map(item => `${value}-${item}`);
  } else {
    return [value];
  }
});

console.log(mappedProducts);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use for in loop

const products = {
  value: {
    phone: [],
    lamp: [],
    car: ["bmw", "toyota", "audi"]
  }
};

const newData = [];

for (const key in products) {
  const value = products[key];

  for (const innerKey in value) {
    const innerVal = value[innerKey];
    if (innerVal.length > 0) {
      innerVal.forEach((item) => newData.push(`${innerKey}-${item}`));
    } else {
      newData.push(innerKey);
    }
  }
}

console.log(newData);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another solution would be to use Object.keys() along with map and concat to get a merged array with keys and values (if any):

const products = {
  value: {
    phone: [],
    lamp: [],
    car: ["bmw", "toyota", "audi"]
  }
};

let keys = [].concat.apply([], Object.keys(products.value).map((key) =>{
    if(products.value[key].length > 0){
    return products.value[key];
  }
  else return key;
}));
console.log(keys);
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