Edit 18.01.21 (see bolt changes)
I'm having problems figuring out a way to loop through a nested array and then to store the output in a desired order. Could you please help me to figure out a way to make this work or to point out what I'm doing wrong?
I think I'm missing a few steps in the process. For example, to break the inner loop at some point and to temporarily store my data. However, I'm not sure at what point... Important to mention, the code is intended to be used in a Google Apps Script
The toy data I'm using looks like this: I added another type of keyword to my toy data – "criss cross" and "bob ross".
var keywords = [ [ ["claude"],["clair"],["carl"], ["criss cross"] ],
[ ["brad"],["bob"],["bill"] ["bob ross"] ] ];
The output I'm hoping to achieve looks like this:
[ [ [ '[claude] '],[ '"claude"' ],[ '+claude' ] ],
[ [ '[clair]' ],[ '"clair"' ],[ '+clair' ] ],
[ [ '[carl]' ],[ '"carl"' ],[ '+carl' ] ],
[ [ '[criss cross]' ],[ '"criss cross"' ], [ '+criss +cross' ] ],
[ [ '[brad]' ],[ '"brad"' ],[ '+brad' ] ],
[ [ '[bob]' ],[ '"bob"' ],[ '+bob' ] ],
[ [ '[bill]' ],[ '"bill"' ],[ '+bill' ] ],
[ [ '[bob ross]' ],[ '"bob ross"' ], [ '+bob +ross' ] ] ]
However, the output I'm creating is the following:
[ [ [ '[claude]' ],[ '[clair]' ],[ '[carl]' ],[ '[brad]' ],[ '[bob]' ],[ '[bill]' ] ],
[ [ '"claude"' ],[ '"clair"' ],[ '"carl"' ],[ '"brad"' ],[ '"bob"' ],[ '"bill"' ] ],
[ [ '+claude' ],[ '+clair' ],[ '+carl' ],[ '+brad' ],[ '+bob' ],[ '+bill' ] ] ]
Here's the code I use:
var keywords = [[["claude"],["clair"],["carl"]],
[["brad"],["bob"],["bill"]]];
var keywords = [].concat.apply( [], keywords );
const PERMUTATION = function permuation( item ) {
var exact = [];
var phrase = [];
var modyfied = [];
for( let i = 0 ; i < item.length ; i++ ) {
var output1 = "[" + item[i] + "]";
exact.push([output1]);
var output2 = '"' + item[i] + '"';
phrase.push([output2]);
var temp = [item[i][0].split(" ").map( i => "+" + i ).join(" ")];
modyfied.push(temp);
}
return new Array( exact, phrase, modyfied ) ;
}
var output = PERMUTATION(keywords);
console.log(output)