编辑代码

function getChoseDishes(arr, numObjects = 3) {
    const selectedObjects = [];
    while (selectedObjects.length < numObjects) { 
        const randomIndex = Math.floor(Math.random() * arr.length); 
        const randomObject = arr[randomIndex]; 
        if (!selectedObjects.includes(randomObject) && randomObject) { 
            selectedObjects.push(randomObject); 
        }
    }
    return selectedObjects
}

// 荤菜
const meatStr = '花甲粉丝、鱼香肉丝、宫保鸡丁、水煮肉片、麻婆豆腐、回锅肉、口水鸡、香辣虾、辣子鸡、肉末茄子、辣椒炒肉'
// 半荤菜
const halfMeatStr = '孜然香肠土豆丁、番茄炒鸡蛋、青椒炒火腿鸡蛋'
// 素菜
const vageStr = '土豆丝、空心菜、红苕颠儿、干煸四季豆、酸辣包菜'
// 汤菜
const soupStr = '番茄鸡蛋汤、黄瓜肉片汤'

const allDishes = {
    "meatDishes": meatStr.split('、'),
    "halfMeatDishes": halfMeatStr.split('、'),
    "vageDishes": vageStr.split('、'),
    "soupDishes": soupStr.split('、')
}

const eatChooses = [
    {
        type: 'meatDishes', // 荤菜
        number: 1, // 数量
    },{
        type: 'halfMeatDishes', // 半荤菜
        number: 0, // 数量
    },{
        type: 'vageDishes', // 素菜
        number: 1, // 数量
    },{
        type: 'soupDishes', // 汤菜
        number: 1, // 数量
    }
]
let chooses = []
eatChooses && eatChooses.map(eat => {
    if(eat.number) {
        chooses = chooses.concat(getChoseDishes(allDishes[eat.type], eat.number))
    }
})

console.log('今天吃:' + chooses.join('、') + '!')