Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

63
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda