Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

83
Visualizações
How to generate a new concatenated multidimensional array from a multidimensional array with dynamic length

How can turn array a to array b?

Or how can I generate a matrix-like multidimensional array based on the array a below?

CONDITIONS: 1. length of array a is dynamic. 2. length of each element in array a are dynamic too

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

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

Example 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 Respostas
Responde à pergunta

0

You need to iterate through the array of arrays and push it one-by-one. Here's the snippet code:

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));

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

about 4 years ago · Juan Pablo Isaza Relatório

0

A simple reduce() can be used to solve your question.

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda