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

66
Visualizações
Combine each element of an array with the ones after it

I am trying to combine items in an array, with every item below it. It should make a set of the current character and each character below it, and iteratively walk down the array. For example, if I have an array like this:

var myArray = ['A','B','C','D']

I would like an output like this:

AB AC AD BC BD CD

The code I have is getting me close, but I am having a hard time figuring out the rest. Here is what I have so far:

var myArray = ['A', 'B', 'C', 'D']
var sql_parts = []
var string = "";
for (var i = 0; i < myArray.length; i++) {
  recurse_function(string, i)
}
console.log(sql_parts)

function recurse_function(string_val, count) {
  if ((myArray.length - count) == 0) {
    return string_val;
  } else {
    string_val += myArray[count]
    sql_parts.push(string_val)
    recurse_function(string_val, count + 1)
  }
}

But this produces:

["A", "AB", "ABC", "ABCD", "B", "BC", "BCD", "C", "CD", "D"]

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I don't think you need to recurse in this particular case. You can simply combine the current element with the following ones with flatMap:

['A','B','C','D'].flatMap((x, i, xs) => xs.slice(i+1).map(y => x+y));
//=> ["AB", "AC", "AD", "BC", "BD", "CD"]
about 4 years ago · Juan Pablo Isaza Relatório

0

Here is one solution:

  • Define the recursive function to take the array and an empty list initially to store the combinations
  • The base condition is when the array is empty or has one element
  • Otherwise, Remove the first element "start"
  • Iterate over the array to store its combinations with its following elements
  • Recur again with array and combinations updated

function recurse_function(array, combinations = []) {
  if(array.length <= 1) return combinations;
  const start = array.shift();
  for(let i = 0; i < array.length; i++) combinations.push(`${start}${array[i]}`);
  return recurse_function(array, combinations);
}

console.log( recurse_function(['A','B','C','D']) );

about 4 years ago · Juan Pablo Isaza Relatório

0

var myArray = ['A','B','C','D']
var sql_parts = []
for(var i =0; i< myArray.length; i++){
  var a = myArray[i]
  for(var j = i+1; j<myArray.length; j++){
    var b=myArray[j]
    var c= a+b
    sql_parts.push(c)
  }
}
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