Create strings using .reduce() in Javascript -


this question has answer here:

i need able take list of strings, , come list of possible combinations of strings combined.

["asdf", "ghj","ew","ptum"] 

it should list this, n

["agep", "aget", "ageu", ... "fjwm"] 

what these?

what asking relatively trivial... loops can easily. below 1 such method use.

var input = ["asdf", "ghj", "ew", "ptum"];    function combinations(r, n) {    var o = [];    if (r.constructor.name === "string")      r = r.split("");    if (n.constructor.name === "string")      n = n.split("");      r.foreach(function(i) {      n.foreach(function(j) {        o.push(i + j);      })    })    return o;  }    document.write("<pre>"+json.stringify(input.reduce(combinations),null,2)+"</pre>");


Comments