简体   繁体   English

创造所有可能性的功能

[英]function that creates all the possibilities

var parameter1 = "String";

var parameter 2 = "-";

function test(parameter1, parameter2) 

var parameter1 = "String"; var parameter1 =“ String”;

var parameter 2 = "-"; var参数2 =“-”;

function test(parameter1, parameter2) 功能测试(参数1,参数2)

result: 结果:

String

S-tring

St-ring

Str-ing

Stri-ng

String

Str-in-g

St-ri-ng

St-ri-ng

St-rin-g

S-tr-ing

S-tr-i-ng

S-tr-ing

S-tr-in-g

S-tri-ng

S-tri-ng

S-trin-g

St-ring

St-r-ing

St-ri-ng

St-ring

St-r-in-g

St-ri-ng

St-ri-ng

St-rin-g

Str-ing

Str-i-ng

Str-ing

Str-in-g

Stri-ng

Stri-ng

Strin-g

**So far tried to achieve this result but nothing is worked any help to get this result is appreciated. **到目前为止,您一直在尝试实现此结果,但是没有任何效果,因此对此表示感谢。 Thank you ** i tried this way but i want to get exact output as above result 谢谢** 我尝试过这种方式,但我想获得上述结果的准确输出

     var array1=["S","T","R","I","N","G"];
      var array2=["-"];
     combos = [] //or combos = new Array(2);

     for(var i = 0; i < array1.length; i++)
    {
     for(var j = 0; j < array2.length; j++)
     {


        combos.push(array1[i] + array2[j])
     }
  console.log(combos);
}

You could take a generator for getting parts of a string and iterate this values by checking if the right side has some character to split. 您可以使用生成器来获取字符串的一部分,并通过检查右侧是否有要分割的字符来迭代此值。 If that is the case call the function again with that string and map new pairs for the result set. 如果是这种情况,请使用该字符串再次调用该函数,并为结果集映射新对。

 function* split(string) { var i = 0; while (++i < string.length) { yield [string.slice(0, i), string.slice(i)]; } } function fn(string, delimiter) { const join = l => r => [l, r].join(delimiter); var result = [string], parts; for (var [left, right] of split(string)) { parts = join(left); if (right.length > 1) { result.push(...fn(right, delimiter).map(parts)); } else { result.push(parts(right)); } } return result; } console.log(fn('String', '-')); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM