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

87
Views
cómo iterar sobre el objeto y asignar dinámicamente valores a las claves

Hola a todos,

entonces estoy luchando con el siguiente problema: tengo un objeto con algunas claves y valores. Me gustaría sacarlos y hacer una nueva matriz de objetos. Aquí está el código, ya que describe mejor mi problema.

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

0

puede iterar en las claves de objeto y usar el método array.map para obtener el objeto esperado

 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 Report

0

Simplemente puede usar Object.values y mapear sobre él

 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 Report

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