How can I generate an array like this:
let result = [
["Training für die Polizei"],
["Training für die Polizisten"],
["Training für die Militär"],
["Training für das Polizei"],
["Training für das Polizisten"],
["Training für das Militär"],
["Trainings für die Polizei"],
["Trainings für die Polizisten"],
["Trainings für die Militär"],
["Trainings für das Polizei"],
["Trainings für das Polizisten"],
...
]
...from an array of words, like the one in the attempt below?
What I have tried:
<pre>let ar = [
["Training", "für", "die", "Polizei"],
["Trainings", "", "das", "Polizisten"],
["Trainingseinheit", "", "", "Militär"]
]
let indexes = [0, 1, 2, 3];
let separators = [' ', ' ', ' ', ' '];
let result = [];
ar = ar.map(x => indexes.map(e => x.slice()[0] = x[e]));
for (let row = 0; row < ar.length; row++) {
let word = ar[row][0];
for (let col = 1; col <= ar[row].length - 1; col++) {
const colLength = ar.map(e => e[col]).filter(e => e != '').length
let sentence = [];
for (let val = 0; val <= colLength; val++) {
let words = ar[val];
if (words != '') {
sentence.push(words)
}
}
result.push(sentence)
}
}
console.log(result)