Мне нужна небольшая помощь, чтобы улучшить функцию, в которой я заполняю массив четырьмя случайными различными неповторяющимися объектами рецептов, используя идентификатор рецептов, но мне нужно добавить дополнительное условие, когда в рецептах должен быть 1 завтрак 1 обед, 1 перекус и 1 ужин, например:
пример JSON с рецептами
[
{
recipeId: 1,
recipeTypeId: 1, // breakFast
"description": "someRecipe",
"img": "someImgBase64",
},
{
recipeId: 2,
recipeTypeId: 2, // lunch
"description": "someRecipe",
"img": "someImgBase64",
},
{
recipeId: 3,
recipeTypeId: 3, // snack
"description": "someRecipe",
"img": "someImgBase64",
},
{
recipeId: 4,
recipeTypeId: 4, // dinner
"description": "someRecipe",
"img": "someImgBase64",
},
{
recipeId: 5,
recipeTypeId: 1, // breakfast
"description": "someRecipe",
"img": "someImgBase64",
}
]
это моя функция:
randomRecipes() {
// previously saved recipes from api
localStorage.getItem('recipesList');
// random generator
const randomIndex = new Set();
const recommendedRandomRecipes: [] = [];
while (randomIndex.size < 4) {
randomIndex.add(Math.floor(Math.random() * recipes.length));
}
randomIndex.forEach(i => recommendedRandomRecipes.push(recipes[Number(i)]));
localStorage.addItem('recommendedRecipes', recommendedRandomRecipes);
this.recipes = recommendedRandomRecipes;
}
результат, который я хочу:
recommendedRandomRecipes [1,2,3,4] or [2,3,4,5]
что я не хочу
recommendedRandomRecipes [1,2,3,5] // 2 breakfast in the array