CookingMatchIngredientsJSON-2.c3p
you are no longer using an array to store ingredient data, there is no need to sort, just compare the Loop name and the amount.
{
"Recipe": {
"TomatoEggSoup": {
"Ingredients": {
"Egg": 2,
"Tomato": 2,
"Water": 10
}
}
},
"Play": {
"Ingredients": {
"Egg": 2
}
}
}
const dataInst = runtime.objects.JSON.getFirstInstance();
const data = dataInst.getJsonDataCopy();
const recipes = data.Recipe;
const playIngredients = data.Play.Ingredients;
let result = "none";
for (const recipeName in recipes) {
const recipeIngredients = recipes[recipeName].Ingredients;
if (Object.entries(recipeIngredients).every(([ingredient, amount]) =>
playIngredients[ingredient] === amount)) {
result = recipeName;
break;
}
}
runtime.setReturnValue(result);