Hello!
I’ve tried getting help elsewhere, but no luck so far.
Given the 2D array of words below, how can I generate sentences, according to the indexes?
let ar = [
["Training", "für", "die", "Polizei"],
["Trainings", "", "das", "Polizisten"],
["Trainingseinheit", "", "", "Militär"]
];
const indexes = [0, 3, 2, 1];
let result = [];
for (let a = 0; a < ar.length; a++) {
let sentence = [];
indexes.forEach(function(index) {
const col = ar.map(e => e[index]).flat();
for (let c = 0; c < col.length; c++) {
if (ar[c] == '') {
c++
continue;
} else {
sentence.push(ar[c]);
}
}
})
result.push(sentence)
}
This is the expected result:
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"],
["Trainings für das Militär"]...
]
I think it needs recursion, but being a beginner, it’s still hard for me. I would appreciate any help!