JS调用函数,所有可能的参数都已排列
JS call function with all possible arguments permuted
考虑以下数组:
var array1 = [true, false];
var array2 = [1, 2];
var array3 = ["a", "b", "c"];
我想用所有参数组合调用函数myFunc(arg1, arg2, arg3)
。但我想避免"foreach"地狱。
有没有可能写函数允许我这样做,所以我可以称之为
cartesianCall(array1, array2, array3, myFunc);
理想情况下使用可变数组计数(myFunc参数)?
编辑:因此函数将被调用:
myFunc(true, 1, "a");
myFunc(true, 1, "b");
myFunc(true, 1, "c");
myFunc(true, 2, "a");
myFunc(true, 2, "b");
myFunc(true, 2, "c");
myFunc(false, 1, "a");
myFunc(false, 1, "b");
myFunc(false, 1, "c");
myFunc(false, 2, "a");
myFunc(false, 2, "b");
myFunc(false, 2, "c");
声明不带参数的函数并使用arguments
关键字:
function cartesianCall() {
for (var i = 0; i < arguments.length; i++) {
// do something with arguments[i]
}
}
http://jsfiddle.net/trevordixon/zEqKy/
function cartesianCall(func, args) {
var combos = allCombos.apply(this, args);
for (var i = 0; i < combos.length; i++) {
func.apply(null, combos[i]);
}
}
function allCombos(first) {
var isArray = toString.call(first) === "[object Array]";
if (!isArray) first = [first]; // Convert non-array to an array with the value
// as the only element
else if (first.length === 0) first = [undefined]; // Convert empty array to an
// array with undefined as
// the only element
if (arguments.length === 1) return first; // base case for recursion
var result = [],
rest = allCombos.apply(this, Array.prototype.slice.call(arguments, 1));
for (var i = 0; i < first.length; i++) {
for (var j = 0; j < rest.length; j++) {
result.push([first[i]].concat(rest[j]));
}
}
return result;
}
然后这样使用:
function printArgs() { console.log('Called with arguments:', arguments); }
cartesianCall(printArgs, [
[true, false],
undefined,
[1, 2],
[],
'a string',
['a', 'b', 'c']
]);
打印:
Called with arguments: [true, undefined, 1, undefined, "a string", "a"]
Called with arguments: [true, undefined, 1, undefined, "a string", "b"]
Called with arguments: [true, undefined, 1, undefined, "a string", "c"]
Called with arguments: [true, undefined, 2, undefined, "a string", "a"]
Called with arguments: [true, undefined, 2, undefined, "a string", "b"]
Called with arguments: [true, undefined, 2, undefined, "a string", "c"]
Called with arguments: [false, undefined, 1, undefined, "a string", "a"]
Called with arguments: [false, undefined, 1, undefined, "a string", "b"]
Called with arguments: [false, undefined, 1, undefined, "a string", "c"]
Called with arguments: [false, undefined, 2, undefined, "a string", "a"]
Called with arguments: [false, undefined, 2, undefined, "a string", "b"]
Called with arguments: [false, undefined, 2, undefined, "a string", "c"]
请注意,空数组的处理方式与undefined
相同。
相关文章:
- 函数参数中的数据与指定变量之间的任何性能差异
- 使用Express捕获参数
- 参数变量出现ngTable指令问题
- AngularJS:我可以跳过函数参数回调吗
- 如何使用skip参数使用angular ui引导进行服务器端分页
- 要求未定义JS回调参数
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何删除多行HTML排列中的空白
- 获取@ResponseBody的一部分作为主干和Spring MVC控制器之间的参数
- jquery设置为使用参数运行
- Javascript”;类“;带有参数的扩展
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 函数未将值作为参数传递
- 如何将参数传递到angularJs中的工厂
- JS:检查URL中的参数,然后迭代一个参数为var的函数
- 正在传递JSONP标头's数据参数到另一个文件中的AJAX请求
- 如何使用排列运算符来获取具有命名参数的函数的所有参数
- 创建具有动态参数排列的方法/函数
- 当参数不按特定顺序排列时,Javascript重新加载具有参数值更改的页面
- JS调用函数,所有可能的参数都已排列