I'm somewhat new JavaScript, but I'm encountering a scenario where I believe I need to use recursion but I'm not exactly sure how to do it. I have a JavaScript object array that contains about 30 Key-Value pairs per record. What I need to do is loop through all of the records and find all possible combinations where the Total Combined product length is below 2280. After I find each individual combination I want to return the combination of Ship Locations from the object. Any help with this would be much appreciated.
This is what the data looks like that I am trying to find combinations for (there actually about 30 key-value pairs per record, only showing the two related to the question):
var arr =[{"shipLOC": "ALASTE", "totalProductLength": 480},
{"shipLOC": "BRONHT", "TotalProductLength": 1520},
{"shipLOC": "ZIHNER", "TotalProductLength": 120},
{"shipLOC": "MEADON", "TotalProductLength": 700},
{"shipLOC": "RUSPOW", "TotalProductLength": 200}]
This code seems to be providing me with all of the combinations that are below the totalTruckLengthAvailable limit of 2280 total length, which is good, but there is additional criteria I need to check before saving the combination and I'm kind of lost on how to do it. There were many combinations of locations provided that may be under 2280 in total length, but still have room for product and could easily use another location to get as close as possible to the 2280 in total length. Is there a way for me to maximize the combinations? Basically I want to keep adding locations while the totalProductLength is below 2280.
function k_combinations(set, k) {
var i, j, combs, head, tailcombs;
// There is no way to take e.g. sets of 5 elements from
// a set of 4.
if (k > set.length || k <= 0) {
return [];
}
// K-sized set has only one K-sized subset.
if (k == set.length) {
return [set];
}
// There is N 1-sized subsets in a N-sized set.
if (k == 1) {
combs = [];
let size = 0;
for (i = 0; i < set.length; i++) {
size = size + set[i].attributes.TotalProductLength
combs.push([set[i]]);
}
if (size < totalTruckLengthAvailable){
return combs;
} else
return [];
}
combs = [];
for (i = 0; i < set.length - k + 1; i++) {
// head is a list that includes only our current element.
head = set.slice(i, i + 1);
// We take smaller combinations from the subsequent elements
tailcombs = k_combinations(set.slice(i + 1), k - 1);
// For each (k-1)-combination we join it with the current
// and store it to the set of k-combinations.
for (j = 0; j < tailcombs.length; j++) {
combs.push(head.concat(tailcombs[j]));
}
}
return combs;
}
function combinations(set) {
var k, i, combs, k_combs;
combs = [];
// Calculate all non-empty k-combinations
for (k = 1; k <= set.length; k++) {
k_combs = k_combinations(set, k);
for (i = 0; i < k_combs.length; i++) {
combs.push(k_combs[i]);
}
}
return combs;
}
console.log(combinations(arr))
The result from the array provided is 8 combinations, as seen in the screenshot below. The problem is that some of these combinations need to be removed as they still have room for more product.