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

96
Views
Agregar elementos de matriz a la clave y los valores del objeto

Si tengo la siguiente matriz

 const Array = ['Michael', 'student', 'John', 'cop', 'Julia', 'actress']

¿Cómo podría hacer un objeto como

 const Object = { Michael: student, John: cop, Julia: actress, }

¿Hay alguna manera de hacer que los elementos de índice par sean las claves del objeto y los elementos de índice impar sean los valores de la clave?

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

0

Un bucle for simple que paso por dos índices a la vez funcionaría.

Prueba como a continuación

 const array = ['Michael', 'student', 'John', 'cop', 'Julia', 'actress']; const output = {} for(let i = 0; i < array.length; i+=2){ output[array[i]] = array[i+1] } console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

 let arr = ['Michael', 'student', 'John', 'cop', 'Julia', 'actress']; let obj={}; arr.forEach((element, index) => { if(index % 2 === 0){ obj[element] = arr[index+1]; } }) console.log(obj);

about 4 years ago · Juan Pablo Isaza Report

0

Puede dividir la matriz en fragmentos de tamaño 2 y luego convertirla en un objeto con Object.fromEntries .

 let arr = ['Michael', 'student', 'John', 'cop', 'Julia', 'actress']; let obj = Object.fromEntries([...Array(arr.length / 2)] .map((_, i)=>arr.slice(i * 2, i * 2 + 2))); console.log(obj);

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!