0

I have these three arrays:

var arr1 = [a1, b1, c1], arr2 = [a2, b2, c2], arr3 = [a3, b3, c3];

How can I combine these arrays so that I can create arrays like the series of arrays in the bottom using jQuery/javascript?

array1 = [a1, a2, a3]
array2 = [a1, a2, b3]
array3 = [a1, a2, c3]
array4 = [a1, b2, a3]
array5 = [a1, b2, b3]
array6 = [a1, b2, c3]
array7 = [a1, c2, a3]
array8 = [a1, c2, b3]
array9 = [a1, c2, c3]

array10 = [b1, a2, a3]
array11 = [b1, a2, b3]
array12 = [b1, a2, c3]
array13 = [b1, b2, a3]
array14 = [b1, b2, b3]
array15 = [b1, b2, c3]
array16 = [b1, c2, a3]
array17 = [b1, c2, b3]
array18 = [b1, c2, c3]

array19 = [c1, a2, a3]
array20 = [c1, a2, b3]
array21 = [c1, a2, c3]
array22 = [c1, b2, a3]
array23 = [c1, b2, b3]
array24 = [c1, b2, c3]
array25 = [c1, c2, a3]
array26 = [c1, c2, b3]
array27 = [c1, c2, c3]
1
  • 3
    add what you have tried so far Commented Jun 6, 2017 at 4:03

1 Answer 1

3

3 nested loops should be all you need...

var arr1 = ['a1', 'b1', 'c1'], arr2 = ['a2', 'b2', 'c2'], arr3 = ['a3', 'b3', 'c3']

var megaArray = []
for (let i = 0, l = arr1.length; i < l; i++) {
  for (let j = 0, m = arr2.length; j < m; j++) {
    for (let k = 0, n = arr3.length; k < n; k++) {
      megaArray.push([arr1[i], arr2[j], arr3[k]])
    }
  }
}
console.info(megaArray)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.