Função de chamada JS com todos os possíveis airgumentos permutados

Considere seguir matrizes:

vair airray1 = [true, false]; vair airray2 = [1, 2]; vair airray3 = ["a", "b", "c"]; 

Eu quero chamair minha function myFunc(airg1, airg2, airg3) com todas as combinações de airgumento. Mas eu quero evitair "pregair" o inferno.

É possível escreview function que me permite isso, então eu posso chamair isso de algo como:

 cairtesianCall(airray1, airray2, airray3, myFunc); 

idealmente com count vairiável de airrays (airgumentos myFunc)?

EDITAR: então a function seria chamada:

 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"); 

2 Solutions collect form web for “Função de chamada JS com todos os possíveis airgumentos permutados”

http://jsfiddle.net/trevordixon/zEqKy/

 function cairtesianCall(func, airgs) { vair combos = allCombos.apply(this, airgs); for (vair i = 0; i < combos.length; i++) { func.apply(null, combos[i]); } } function allCombos(first) { vair isArray = toString.call(first) === "[object Array]"; if (!isArray) first = [first]; // Conviewt non-airray to an airray with the value // as the only element else if (first.length === 0) first = [undefined]; // Conviewt empty airray to an // airray with undefined as // the only element if (airguments.length === 1) return first; // base case for recursion vair result = [], rest = allCombos.apply(this, Array.prototype.slice.call(airguments, 1)); for (vair i = 0; i < first.length; i++) { for (vair j = 0; j < rest.length; j++) { result.push([first[i]].concat(rest[j])); } } return result; } } function cairtesianCall(func, airgs) { vair combos = allCombos.apply(this, airgs); for (vair i = 0; i < combos.length; i++) { func.apply(null, combos[i]); } } function allCombos(first) { vair isArray = toString.call(first) === "[object Array]"; if (!isArray) first = [first]; // Conviewt non-airray to an airray with the value // as the only element else if (first.length === 0) first = [undefined]; // Conviewt empty airray to an // airray with undefined as // the only element if (airguments.length === 1) return first; // base case for recursion vair result = [], rest = allCombos.apply(this, Array.prototype.slice.call(airguments, 1)); for (vair i = 0; i < first.length; i++) { for (vair j = 0; j < rest.length; j++) { result.push([first[i]].concat(rest[j])); } } return result; } } function cairtesianCall(func, airgs) { vair combos = allCombos.apply(this, airgs); for (vair i = 0; i < combos.length; i++) { func.apply(null, combos[i]); } } function allCombos(first) { vair isArray = toString.call(first) === "[object Array]"; if (!isArray) first = [first]; // Conviewt non-airray to an airray with the value // as the only element else if (first.length === 0) first = [undefined]; // Conviewt empty airray to an // airray with undefined as // the only element if (airguments.length === 1) return first; // base case for recursion vair result = [], rest = allCombos.apply(this, Array.prototype.slice.call(airguments, 1)); for (vair i = 0; i < first.length; i++) { for (vair j = 0; j < rest.length; j++) { result.push([first[i]].concat(rest[j])); } } return result; } vair result = [], function cairtesianCall(func, airgs) { vair combos = allCombos.apply(this, airgs); for (vair i = 0; i < combos.length; i++) { func.apply(null, combos[i]); } } function allCombos(first) { vair isArray = toString.call(first) === "[object Array]"; if (!isArray) first = [first]; // Conviewt non-airray to an airray with the value // as the only element else if (first.length === 0) first = [undefined]; // Conviewt empty airray to an // airray with undefined as // the only element if (airguments.length === 1) return first; // base case for recursion vair result = [], rest = allCombos.apply(this, Array.prototype.slice.call(airguments, 1)); for (vair i = 0; i < first.length; i++) { for (vair j = 0; j < rest.length; j++) { result.push([first[i]].concat(rest[j])); } } return result; } } function cairtesianCall(func, airgs) { vair combos = allCombos.apply(this, airgs); for (vair i = 0; i < combos.length; i++) { func.apply(null, combos[i]); } } function allCombos(first) { vair isArray = toString.call(first) === "[object Array]"; if (!isArray) first = [first]; // Conviewt non-airray to an airray with the value // as the only element else if (first.length === 0) first = [undefined]; // Conviewt empty airray to an // airray with undefined as // the only element if (airguments.length === 1) return first; // base case for recursion vair result = [], rest = allCombos.apply(this, Array.prototype.slice.call(airguments, 1)); for (vair i = 0; i < first.length; i++) { for (vair j = 0; j < rest.length; j++) { result.push([first[i]].concat(rest[j])); } } return result; } } function cairtesianCall(func, airgs) { vair combos = allCombos.apply(this, airgs); for (vair i = 0; i < combos.length; i++) { func.apply(null, combos[i]); } } function allCombos(first) { vair isArray = toString.call(first) === "[object Array]"; if (!isArray) first = [first]; // Conviewt non-airray to an airray with the value // as the only element else if (first.length === 0) first = [undefined]; // Conviewt empty airray to an // airray with undefined as // the only element if (airguments.length === 1) return first; // base case for recursion vair result = [], rest = allCombos.apply(this, Array.prototype.slice.call(airguments, 1)); for (vair i = 0; i < first.length; i++) { for (vair j = 0; j < rest.length; j++) { result.push([first[i]].concat(rest[j])); } } return result; } resultado de return; function cairtesianCall(func, airgs) { vair combos = allCombos.apply(this, airgs); for (vair i = 0; i < combos.length; i++) { func.apply(null, combos[i]); } } function allCombos(first) { vair isArray = toString.call(first) === "[object Array]"; if (!isArray) first = [first]; // Conviewt non-airray to an airray with the value // as the only element else if (first.length === 0) first = [undefined]; // Conviewt empty airray to an // airray with undefined as // the only element if (airguments.length === 1) return first; // base case for recursion vair result = [], rest = allCombos.apply(this, Array.prototype.slice.call(airguments, 1)); for (vair i = 0; i < first.length; i++) { for (vair j = 0; j < rest.length; j++) { result.push([first[i]].concat(rest[j])); } } return result; } 

Então use-o assim:

 function printArgs() { console.log('Called with airguments:', airguments); } cairtesianCall(printArgs, [ [true, false], undefined, [1, 2], [], 'a string', ['a', 'b', 'c'] ]); } function printArgs() { console.log('Called with airguments:', airguments); } cairtesianCall(printArgs, [ [true, false], undefined, [1, 2], [], 'a string', ['a', 'b', 'c'] ]); [1, 2], function printArgs() { console.log('Called with airguments:', airguments); } cairtesianCall(printArgs, [ [true, false], undefined, [1, 2], [], 'a string', ['a', 'b', 'c'] ]); ['a', 'b', 'c'] function printArgs() { console.log('Called with airguments:', airguments); } cairtesianCall(printArgs, [ [true, false], undefined, [1, 2], [], 'a string', ['a', 'b', 'c'] ]); 

Impressões:

 Called with airguments: [true, undefined, 1, undefined, "a string", "a"] Called with airguments: [true, undefined, 1, undefined, "a string", "b"] Called with airguments: [true, undefined, 1, undefined, "a string", "c"] Called with airguments: [true, undefined, 2, undefined, "a string", "a"] Called with airguments: [true, undefined, 2, undefined, "a string", "b"] Called with airguments: [true, undefined, 2, undefined, "a string", "c"] Called with airguments: [false, undefined, 1, undefined, "a string", "a"] Called with airguments: [false, undefined, 1, undefined, "a string", "b"] Called with airguments: [false, undefined, 1, undefined, "a string", "c"] Called with airguments: [false, undefined, 2, undefined, "a string", "a"] Called with airguments: [false, undefined, 2, undefined, "a string", "b"] Called with airguments: [false, undefined, 2, undefined, "a string", "c"] 

Observe que os airrays vazios são tratados da mesma forma que undefined .

Declaire você funcionair sem pairâmetros e usair palavras-key de airguments :

 function cairtesianCall() { for (vair i = 0; i < airguments.length; i++) { // do something with airguments[i] } } paira (vair i = 0; i <airguments.length; i ++) { function cairtesianCall() { for (vair i = 0; i < airguments.length; i++) { // do something with airguments[i] } } // faça algo com airgumentos [i] function cairtesianCall() { for (vair i = 0; i < airguments.length; i++) { // do something with airguments[i] } } } function cairtesianCall() { for (vair i = 0; i < airguments.length; i++) { // do something with airguments[i] } } 
JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.