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

86
Views
Cómo generar una nueva matriz multidimensional concatenada a partir de una matriz multidimensional con longitud dinámica

¿Cómo se puede convertir la matriz a en una matriz b ?

¿O cómo puedo generar una matriz multidimensional similar a una matriz basada en la matriz a continuación?

CONDICIONES: 1. la longitud de la matriz a es dinámica . 2. la longitud de cada elemento en la matriz a también es dinámica

 const a = [ ['red', 'blue'], ['small', 'medium', 'large'], ] const b = [ ['red', 'small'], ['red', 'medium'], ['red', 'large'], ['blue', 'small'], ['blue', 'medium'], ['blue', 'large'], ]

Ejemplo 2:

 const a = [ ['quadcore'], ['4GB', '8GB'], ['black', 'grey'], ] const b = [ ['quadcore', '4GB', 'black'], ['quadcore', '4GB', 'grey'], ['quadcore', '8GB', 'black'], ['quadcore', '8GB', 'grey'], ]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe iterar a través de la matriz de matrices y empujarla una por una. Aquí está el código del fragmento:

 function print(arr) { let n = arr.length; let result = []; let indices = new Array(n); for(let i = 0; i < n; i++) indices[i] = 0; while (true) { // Print current combination let tmp = []; for(let i = 0; i < n; i++){ tmp.push(arr[i][indices[i]]); } result.push(tmp); let next = n - 1; while (next >= 0 && (indices[next] + 1 >= arr[next].length)) next--; // No such array is found so no more // combinations left if (next < 0) break; // If found move to next element in that // array indices[next]++; // For all arrays to the right of this // array current index again points to // first element for(let i = next + 1; i < n; i++) indices[i] = 0; } return result; } const a = [ ['quadcore'], ['4GB', '8GB'], ['black', 'grey'], ]; console.log(print(a));

Referencias: https://www.geeksforgeeks.org/combinations-from-n-arrays-picking-one-element-from-each-array/

about 4 years ago · Juan Pablo Isaza Report

0

Se puede usar un simple reduce() para resolver su pregunta.

 const arr = [ ["red", "blue"], ["small", "medium", "large"], ]; const output = arr.reduce((acc, cur) => { const store = []; for (const a of acc) { for (const c of cur) { if (Array.isArray(a)) { store.push([...a, c]); } else { store.push([a, c]); } } } return store; }); console.log(output);

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!