Updated on 18th January, 2021 (refer to bold changes)
I'm currently facing difficulties while attempting to iterate through a nested array and then organize the output in a specific order. Can you assist me in finding a solution to make this work or point out any mistakes I might be making?
It seems like there are certain steps missing in my process. For instance, knowing when to break the inner loop and temporarily store the data. However, I'm uncertain about the exact point... It's worth mentioning that the code is meant for use in a Google Apps Script
The dummy data I am working with is structured as follows: I've introduced a new set of keywords to my sample data – "criss cross" and "bob ross".
var keywords = [ [ ["claude"],["clair"],["carl"], ["criss cross"] ],
[ ["brad"],["bob"],["bill"], ["bob ross"] ] ];
The expected output that I would like to obtain appears 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 actual output I'm generating is as follows:
[ [ [ '[claude]' ],[ '[clair]' ],[ '[carl]' ],[ '[brad]' ],[ '[bob]' ],[ '[bill]' ] ],
[ ["claude"],["clair"],["carl"],["brad"],["bob"],["bill"] ],
[ '+claude','+clair', '+carl', '+brad', '+bob', '+bill' ] ]
Here's the code I am using:
var keywords = [[[ "claude"],["clair"],["carl"]],
[[ "brad"],["bob"],["bill"]]];
var keywords = [].concat.apply( [], keywords );
const PERMUTATION = function permuation( item ) {
var exact = [];
var phrase = [];
var modified = [];
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(" ")];
modified.push(temp);
}
return new Array( exact, phrase, modified ) ;
}
var output = PERMUTATION(keywords);
console.log(output)