I want to copy recipe with same ingredients, but with varying quantities of each ingredient.
Here is my ingredients object:
const ingredientsToStretch2 = [
{
productName: "Strawberries, raw",
amount: 350,
numberOfServings: 350,
servingQuantity: 1,
incrementSize: 2,
maxAmount: 354,
},
{
productName: "Blueberries, raw",
amount: 100,
numberOfServings: 100,
servingQuantity: 1,
incrementSize: 2,
maxAmount: 104,
},
{
productName: "Blackberries, raw",
numberOfServings: 100,
amount: 100,
servingQuantity: 1,
incrementSize: 2,
maxAmount: 104,
},
];
incrementSize is the number that I want to increase the amount by, maxAmount is the amount where I want to stop incrementing that ingredient.
So far, I created a loop that puts all single ingredient variations into array:
const handleStretch = () => {
let stretchedRecipes = [];
for (let i = 0; i < ingredientsToStretch.length; i++) {
let combinations = [];
const gramIncrementSize = ingredientsToStretch[i].incrementSize;
const maxGramSize = ingredientsToStretch[i].maxAmount;
const initialNumberOfServings =
ingredientsToStretch[i].numberOfServings + gramIncrementSize;
for (
let j = initialNumberOfServings;
j <= maxGramSize;
j += gramIncrementSize
) {
combinations.push({
productName: ingredientsToStretch[i].productName,
numberOfServings: j,
});
}
stretchedRecipes.push(combinations);
}
};
This gives me this result:
[
[
{
productName: "Strawberries, raw",
numberOfServings: 352,
},
{
productName: "Strawberries, raw",
numberOfServings: 354,
},
],
[
{
productName: "Blueberries, raw",
numberOfServings: 102,
},
{
productName: "Blueberries, raw",
numberOfServings: 104,
},
],
[
{
productName: "Blackberries, raw",
numberOfServings: 102,
},
{
productName: "Blackberries, raw",
numberOfServings: 104,
},
],
];
Now, how do I create all possible combinations out of this array?
example of copies:
- same strawberries, numberOfServings+2 on blueberries , same blackberries
- same strawberries, numberOfServings+2 on blueberries , same blackberries
- same strawberries, same blueberries , numberOfServings+2 on blackberries
- same strawberries, same blueberries , numberOfServings+2 on blackberries