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

151
Views
JavaScript: combina dos matrices con diferentes tamaños

Tengo dos matrices:

 var array1= [1,3,5,7,9,11] var array2= [undefined,4,6]

Necesito fusionar un elemento de la matriz1, luego un elemento de la matriz2, etc. Este es mi código:

 function mergeArrays(array1, array2){ var array3 = []; maxlength = Math.max(array1.length, array2.length); for(i=0;i<maxlength;i++){ array3.push(array1[i]); array3.push(array2[i]); } return console.log(array3); }

La salida ahora es:

 array3 = [1,undefined,3,4,6,7,undefined,9,undefined,11,undefined]

Necesito que la salida sea:

 array3 = [1,undefined,3,4,6,7,8,11]

Quiero decir, no puedo usar (! = indefinido), porque si tengo un indefinido en el medio de la matriz, tiene que estar allí.

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

0

No está colocando una marca para la longitud de una matriz más corta. Su función está obteniendo un valor que es más alto que la longitud de la matriz, por lo tanto, extra indefinido. Esto debería hacer el trabajo

 var array1 = [1, 3, 5, 7, 9, 11]; var array2 = [undefined, 4, 6]; function mergeArrays(array1, array2) { var array3 = []; maxlength = Math.max(array1.length, array2.length); for (i = 0; i < maxlength; i++) { if (i < array1.length) array3.push(array1[i]); if (i < array2.length) array3.push(array2[i]); } return console.log(array3); } mergeArrays(array1, array2);

about 4 years ago · Juan Pablo Isaza Report

0

En lugar de verificar undefined , puede verificar las longitudes de la matriz, así:

 const array1 = [1,3,5,7,9,11] const array2 = [undefined,4,6] function mergeArrays(array1, array2){ const array3 = []; const maxlength = Math.max(array1.length, array2.length); for(let i = 0; i < maxlength; i++) { if (i < array1.length) { array3.push(array1[i]); } if (i < array2.length) { array3.push(array2[i]); } } return array3; } console.log(mergeArrays(array1, array2));

about 4 years ago · Juan Pablo Isaza Report

0

Puede tomar la longitud máxima de las dos matrices y hacer un bucle hasta que alcance ese valor. Mientras realiza el bucle, verifique si se ha excedido la longitud y empuje hacia el resultado.

 const allEqual = (...args) => ((head, ...tail) => tail.every(curr => curr === head)) (args.map(v => JSON.stringify(v))); const test = ({ actual, expected }) => { console.log(JSON.stringify(actual)); console.log(allEqual(actual, expected)); }; const interlaceArrays = (...arrays) => { const result = []; const maxLen = Math.max(...arrays.map(({ length }) => length)); for (let i = 0; i < maxLen; i++) { arrays.forEach(arr => { if (i < arr.length) result.push(arr[i]); }) } return result; }; const odd = [1, 3, 5, 7, 9, 11], even = [undefined, 4, 6], other = ['a', 'b']; test({ actual: interlaceArrays(odd, even), expected: [1, undefined, 3, 4, 5, 6, 7, 9, 11] }); test({ actual: interlaceArrays(odd, even, other), expected: [1, undefined, 'a', 3, 4, 'b', 5, 6, 7, 9, 11] });
 .as-console-wrapper { top: 0; max-height: 100% !important; }

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!