I'm working on a data manipulation project - where I have to map through an array of arrays and export a single string of all possibilities within these arrays.
For example:
const array = [
[{id: 1}, {id: 2}], [{id: "a"}, {id: "b"}], [{id: "string"}]
]
Expected output is to have:
const newArray = ["1_a_string", "2_a_string", "1_b_string", "2_b_string"]
I'm having challenges in having all permutations knowing:
Appreciate if anyone can guide me to the right path to solve it.
Thanks
you can write a function like this and you can manipulate this function as your requirement . that means if you want concated string something like that..
function print(arr)
{
let n = arr.length;
let indices = new Array(n);
for(let i = 0; i < n; i++)
indices[i] = 0;
while (true)
{
for(let i = 0; i < n; i++)
document.write(
arr[i][indices[i]].id + " ");
document.write("<br>");
let next = n - 1;
while (next >= 0 && (indices[next] + 1 >=
arr[next].length))
next--;
if (next < 0)
return;
indices[next]++;
for(let i = next + 1; i < n; i++)
indices[i] = 0;
}
}
// Driver code
const array = [
[{id: 1}, {id: 2}], [{id: "a"}, {id: "b"}], [{id: "string"}]
]
print(array);
Here is codepan link