调用循环函数-我缺少什么?

时间:2020-08-02 19:50:29

标签: javascript node.js function loops for-loop

所以我在对象内部有一个函数,可以随机选择4个练习之一。

    this.lift = function (number, group) {
        const random = Math.floor(Math.random() * Math.floor(number));
        const pick = group[random]
        return pick
    }

然后我有与每个组相对应的练习阵列供其选择

 this.chest = ["Bench Press", "Incline Bench Press", "Weighted Dips", "Chest Fly"]
 this.back = ["Wide Grip Pulldown", "Close Grip Pulldown", "Barbell Row", "Seated/Supported Row"]

etc. etc. etc.

我可以单独调用该函数为每个组选择一个练习,如下所示:

console.log(work.lift(4, work.chest))
console.log(work.lift(4, work.back))
etc. etc.

但这有点丑陋,有时会一次调用该函数24次以上。所以我只想使其运行在一个干净的循环上。所以我有这个:

            const arnie = ["chest", "back", "delt", "bicep", "tricep", "quad", "ham", "core"]

            for (const muscle of arnie) {
                console.log(work.lift(4, whatgoeshere))
            }

但是我无法使它正常工作,在循环中使用var i尝试了work.muscle,work.arnie [i],但没有一个运行。我一定很想念一些小东西。那怎么了?

1 个答案:

答案 0 :(得分:2)

muscle变量引用了肌肉群,您可以使用它来索引work

work.lift(4, work[muscle])

请参见property accessors