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

68
Views
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 answers
Answer question

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 Report

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 Report

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 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!