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

179
Views
Cómo crear una matriz de objetos a partir de dos longitudes diferentes de matriz javascript

Cómo crear una matriz de objetos a partir de dos longitudes diferentes de matriz

por ejemplo

 arr1 = ["first","second","third","fourth","fifth","Sixth"] arr2 = [["1","2","3","4","5","6"],["7","8","9","10","11","12"],["1","2","3","4"]] finalArray = [{ first:1, second:2 third:3, fourth:4, fifth:5, sixth:6 },{ first:7, second:8 third:9, fourth:10, fifth:11, sixth:12 }]

Intenté esto usando el mapa pero obteniendo cada par de valores clave como un objeto completo

ejemplo

 [ {first: 1} {second: 2} {third: 3} {fourth: 4} ]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Con map() y reduce() :

 const arr1 = ["first", "second", "third", "fourth", "fifth", "Sixth"]; const arr2 = [["1", "2", "3", "4", "5", "6"], ["7", "8", "9", "10", "11", "12"], ["1", "2", "3", "4"]]; const res = arr2.map(v => v.reduce((a, v, i) => ({...a, [arr1[i]]: v}), {})); console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Puede aprovechar Array.prototype.reduce para actualizar la forma de la matriz de resultados

 let arr1 = ["first","second","third","fourth","fifth","Sixth"]; let arr2 = [["1","2","3","4","5","6"],["7","8","9","10","11","12"],["1","2","3","4"]]; let result = arr2.reduce((accumulator, current) => { let obj = arr1.reduce((acc, currentKey, index) => { if(current.indexOf(index) && current[index] !== undefined ){ acc[[currentKey]] = current[index]; } return acc; }, {}); return accumulator.concat(obj); }, []); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

sin reduce() y caso de borde cubierto cuando el arr1 contiene menos elementos que el elemento de arr2

 const arr1 = ["first","second","third","fourth","fifth","Sixth"] const arr2 = [["1","2","3","4","5","6"],["7","8","9","10","11","12"],["1","2","3","4"]] const res = arr2.map(values => { const res = {} for(const [index, value] of arr1.entries()){ if(values[index]) { res[value] = values[index] // or parseInt(values[index]) } else { break } } return res }) console.dir(res)

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!