I have an array with the following structure:
var array = [
[
[3, 5], //a
[8, 2], //b
[5, 3], //c
[8, 1] //d
],
[
[3, 9], //a
[7, 4], //b
[5, 6], //c
[8, 8] //d
]
]
I wanna get all possible combinations of the sum of the second numbers of 4 parts (a,b,c,d) using everything data from the array (It's hard to formulate, sorry).
Example: Take the second numbers of a, b, c and d of arrays and sum them (5 + 2 + 3 + 1 = 11). After that we take 'b' second number from second array ([x, 2] -> [x, 4]) and sum again (5 + 4 + 3 + 1 = 13), then don't change 'b', take 'c' second number of second array and sum etc.
If we wanna change 'a' second number, we have to take 'a' second number from the second array - we can't use 'b' second number of second array instead of 'a'. 1st a -> 2nd a, 1st b -> 2nd b, 1st c -> 2nd c, 1st d -> 2nd d.
[3, 5], [3, 5],
[8, 2], -> [n, 4] = [8, 4],
[5, 3], [5, 3],
[8, 1] [8, 1]
11 13
___________________________
[3, 5], [3, 5],
[8, 4], [7, 4],
[5, 3], -> [n, 6] = [5, 6],
[8, 1] [8, 1]
13 16
So there are 16 possible combinations.
I made code using 4 'for...in' statements and it works fine, but I think there is a better way to solve this task (to use less code or to refactor nested statements) - and I don't know which one.
My solution:
var array = [
[
[3, 5], //a
[8, 2], //b
[5, 3], //c
[8, 1] //d
],
[
[3, 9], //a
[7, 4], //b
[5, 6], //c
[8, 8] //d
]
]
var sum = 0,
id = 1;
for (var a in array) {
for (var b in array) {
for (var c in array) {
for (var d in array) {
sum = array[a][0][1] +
array[b][1][1] +
array[c][2][1] +
array[d][3][1];
console.log('ID: ' + id++ + " Sum = " + sum);
}
}
}
}
Output:
ID: 1 Sum = 11
ID: 2 Sum = 18
ID: 3 Sum = 14
ID: 4 Sum = 21
ID: 5 Sum = 13
ID: 6 Sum = 20
ID: 7 Sum = 16
ID: 8 Sum = 23
ID: 9 Sum = 15
ID: 10 Sum = 22
ID: 11 Sum = 18
ID: 12 Sum = 25
ID: 13 Sum = 17
ID: 14 Sum = 24
ID: 15 Sum = 20
ID: 16 Sum = 27
I wanna know is there the best way to solve this task? Can I change 4 'for...in' statements to something better? How would you solve this task?
I just wanna change
for (var a in array) {
for (var b in array) {
for (var c in array) {
for (var d in array) {
/* ... */
}
}
}
}
to something better.
P.S. Also quantity of 'for...in' statements depends on quantity of 'parts' in last array.
a, b, c, d = 4 parts
4 parts = 4 'for...in' statements