简体   繁体   English

如何在我的函数参数中使对象数组调用每个对象的函数?

[英]How can I make an array of object in my function parameter call a function for each object?

I created the following checkGrid function which takes a count and then another two parameters. 我创建了以下checkGrid函数,该函数先进行计数,然后再进行另外两个参数的处理。 It's a solution which hardcodes in the selectOption calls and is not very flexible: 这是一个在selectOption调用中进行硬编码的解决方案,并且不够灵活:

checkGrid = function(expectCount, typeParam, selectParam) {

   it('Check', function () {
      selectOption('examTypeSelect', typeParam).click();
      selectOption('examStatusSelect', selectParam).click();
    });

}
checkGrid(10, '*', '*');

Now I would like to make this more flexible so that it accepts arguments for any number of selectOption functions. 现在,我想使其更加灵活,以便它接受任意数量的selectOption函数的参数。 So I was thinking of something that I could call like this. 所以我在想可以这样称呼的东西。 HEre what I would need is for the checkGrid function to call the selectOption three times: 在这里,我需要的是checkGrid函数调用selectOption三次:

checkGrid(10, [{'examTypeSelect','*'}
               {'examStatusSelect', '*'},
               {'abcSelect','29'}]);

How could I take an array object that's the second parameter for checkGrid and make it so that it calls any number of selectOption functions depending on how many elements there are in the array? 我怎样才能接受作为checkGrid的第二个参数的数组对象,并使其根据数组中有多少个元素来调用任意数量的selectOption函数?

By using a loop over the array and .apply to call each function with a variable number of arguments. 通过在数组上循环并使用.apply调用具有可变数量参数的每个函数。

checkGrid = function(expectCount, stuff) {
    it('Check', function () {
        for (var i = 0; i < stuff.length; ++i) {
            selectOption.apply(this, stuff[i]).click();
        }
    });
}

You would call this not with your proposed syntax (which is invalid), but like 您不会使用建议的语法来调用它(这是无效的),而是

checkGrid(10, [
                ['examTypeSelect','*'],
                ['examStatusSelect', '*'],
                ['abcSelect','29','and','perhaps','more','parameters']
              ]
          );

Finally, it's unclear what expectCount is supposed to do but I left it there because your original code presumably does something with it. 最后,目前尚不清楚expectCount应该做些什么,但是我把它留在那里了,因为您的原始代码大概可以对它做些什么

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每次执行 function 时,如何将数组另存为新的 object 而不会覆盖我的旧数组 - how can you make an array save as a new object each time a function is executed and not overwrite my old array 如何在函数调用时访问数组中的对象 - how to access the object in my array on function call 如何使数组与 intersects() function 中的 object 交互? - how can i make an array interact with a object in a intersects() function? 为promise结果对象的每个数组元素调用异步函数,进行更改并返回此对象 - call async function for each array element of a promise result object, make changes and return this object 如何使这个函数面向对象,以便我可以向我的对象添加一个新位置并且它可以在我的函数中工作? - How do I make this function object-oriented so that I can just add a new location to my object and it will work in my function? 如何调用带有对象和数组的函数并将其添加到该对象的属性? - How can I call a function with an object and array and have it add properties to the object? 将对象数组简化为单个对象,并为每个单个对象调用一个函数 - Reduce array of objects to a single object and call a function for each individual object 我是如何通过这样的参数函数将对象形成一个数组的 - how i get the object form an array with a parameter function like this 我可以通过 object 作为 onClick function 的参数吗 - Can I pass an object as the parameter for an onClick function 如何在调用 function 时使用 object 破坏作为参数? - How can I use object destruction as parameter in calling a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM