I have a nested array that contains several strings at different levels.
let nested = [
"ONE",
[
"TWO",
"THREE",
[
"FOUR",
"FIVE"
],
"SIX",
"SEVEN"
],
[
"HEIGHT"
],
"NINE"
]
I have a working function that can traverse my nested array to retrieve a value, using an array of indexes as "needle":
const getValueByIndexes = (array,indexes) => {
const children = array[indexes[0]];
if(indexes.length > 1){
return getValueByIndexes(children,indexes.slice(1));
}else{
return children;
}
}
let test = getValueByIndexes(nested,[1,2,1]);
console.log(test); //this IS returning "FIVE", as expected
This works well; but my question here is that I need a function that would BUILD those needles; based on my nested array.
The output I want is:
[
[0],//ONE
[1,0],//TWO
[1,1],//THREE
[1,2,0],//FOUR
[1,2,1],//FIVE
[1,3],//SIX
[1,4],//SEVEN
[2,0],//HEIGHT
[3],//NINE
]
How could I achieve that ? Thanks !