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

90
Vistas
how to iterate over object and dynamically assign values to keys

Hella all,

so I am struggling with the following problem: I have an object with some keys and values. I would like to take them out and make a new array of objects. Here is the code, as it describes my problem better.

const sourceObj = {
  test1: "a",
  test2: "b",
  test3: "c",
  test4: "d",
  test5: "e"
};

const myTable = [];

for (let value of sourceObj) {
  for (let i = 1; i < 6; i++) {
    const targetObject = {
      foo: "foo",
      year: value.test + i,
      bar: "bar"
    };
    myTable.push(targetObject);
  }
}

console.log(myTable);

// expected output
// [
//   {
//     foo: "foo",
//     year: "a",
//     bar: "bar"
//   },
//   {
//     foo: "foo",
//     year: "b",
//     bar: "bar"
//   },
//   ...
//   {
//     foo: "foo",
//     year: "e",
//     bar: "bar"
//   },
// ]

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

0

you can iterate on object keys and use method array.map to get the expected object

const sourceObj = {
  test1: "a",
  test2: "b",
  test3: "c",
  test4: "d",
  test5: "e"
};

const myTable = Object.keys(sourceObj).map((key) => {
  return {
      foo: "foo",
      year: sourceObj[key],
      bar: "bar"
    };
});

console.log(myTable);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can simply use Object.values and map over it

const sourceObj = {
  test1: "a",
  test2: "b",
  test3: "c",
  test4: "d",
  test5: "e"
};

const myTable = Object.values(sourceObj).map(year => ({
  foo: "foo",
  year,
  bar: "bar"
}))
console.log(myTable);

about 4 years ago · Juan Pablo Isaza Denunciar

0

const sourceObj = {
  test1: "a",
  test2: "b",
  test3: "c",
  test4: "d",
  test5: "e"
};

const myTable = [];

for(item in sourceObj){
  myTable.push(
    {
      foo : "foo",
      year : sourceObj[item],
      bar : "bar"
    }
  );
}

console.log(myTable);

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