JS循环中遍历目标数组实现4周餐单随机洗牌展示
实现方案
你的现有逻辑已经完成了单数组打乱、每日特餐输出的核心功能,只需要调整数组取值逻辑,就能实现4周分别对应4个不同餐单数组的需求,不需要改动已写好的menu对象、shuffle打乱函数。
具体修改点
- 在所有餐单数组(
meals/meals1/meals2/meals3)定义完成后,新增一个按周顺序存储餐单的数组:
// 索引0对应第1周餐单,索引3对应第4周餐单 const weeklyMealLists = [meals, meals1, meals2, meals3];
- 替换原有的周循环逻辑:不再固定操作
meals数组,每次周循环根据索引取对应周的餐单,打乱后输出即可。同时把原在内层循环的数组长度校验移到外层,减少冗余判断。
修改后可直接运行的完整代码
const menu = { _meal: "", _price: 0, // 仅当传入值为字符串时更新餐品名称 set meal(mealToCheck) { if (typeof mealToCheck === "string") { return (this._meal = mealToCheck); } }, // 仅当传入值为数字时更新餐品价格 set price(priceToCheck) { if (typeof priceToCheck === "number") { return (this._price = priceToCheck); } }, // 餐品和价格都合法时返回特餐文案,否则返回错误提示 get todaysSpecial() { if (this._meal && this._price) { return `'s Special is ${this._meal}, for just ${this._price}£!!`; } else { return "Meal and Price wasn't entered correctly!!"; } } }; // 各周餐品及对应价格数组 let meals = [ {name: "Pizza", price: 9}, {name: "Steak", price: 13}, {name: "Pie", price: 11}, {name: "Roast", price: 14}, {name: "Moussaka", price: 9}, {name: "Lasagne", price: 10}, {name: "Tacos", price: 9} ]; let meals1 = [ {name: "Omelette", price: 9}, {name: "Kebab", price: 13}, {name: "Ratatouille", price: 14}, {name: "Quiche", price: 9}, {name: "Burgers", price: 9}, {name: "Hot-Dogs", price: 10}, {name: "Burritos", price: 9} ]; let meals2 = [ {name: "Bourguignon", price: 10}, {name: "Stew", price: 14}, {name: "Fish&Chips", price: 15}, {name: "Fried Chicken", price: 10}, {name: "Cannelloni", price: 10}, {name: "Crepes", price: 11}, {name: "Gyros", price: 10} ]; let meals3 = [ {name: "Greek Salad", price: 11}, {name: "Seafood", price: 15}, {name: "Falafel", price: 16}, {name: "Wraps", price: 11}, {name: "Souvlaki", price: 11}, {name: "Shawarma", price: 12}, {name: "Quesadilla", price: 11} ] // 按周顺序存储餐单的容器 const weeklyMealLists = [meals, meals1, meals2, meals3]; // 数组随机打乱函数 const shuffle = (array) => { let currentIndex = array.length, randomIndex; while (currentIndex !== 0) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; [array[currentIndex], array[randomIndex]] = [ array[randomIndex], array[currentIndex] ]; } return array; } let week = ["Week 1", "Week 2", "Week 3", "Week 4"]; const dayOfTheWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] // 周循环输出餐单 for (let w = 0; w < week.length; w++) { console.log("\n" + week[w]) // 对当前周的餐单做浅拷贝后打乱,避免修改原始数组数据 const currentWeekMeals = shuffle([...weeklyMealLists[w]]); // 提前校验餐品数量和一周天数是否匹配 if (currentWeekMeals.length !== dayOfTheWeek.length) { const diff = Math.abs(currentWeekMeals.length - dayOfTheWeek.length); if (currentWeekMeals.length < dayOfTheWeek.length) { console.log(`You need to add ${diff} meal(s) to the list!!!`) } else { console.log(`You need to remove ${diff} meal(s) from the list!!!`) } continue; } // 遍历输出每日特餐 for (let i = 0; i < currentWeekMeals.length; i++) { menu.meal = currentWeekMeals[i].name; menu.price = currentWeekMeals[i].price; console.log(dayOfTheWeek[i] + menu.todaysSpecial); } }
补充说明
- 代码里用
[...weeklyMealLists[w]]做浅拷贝,是为了避免打乱操作修改最开始定义的原始餐单数组,每次运行代码都能得到基于原始餐单的全新随机结果 - 把长度校验移出天循环后,避免了重复判断,执行逻辑更顺畅
内容的提问来源于stack exchange,提问作者Giovanni Farina
相关产品推荐
相关产品推荐

