How to convert the index of a flatten multidimensional array (any depth) to the original index?

Issue

This Content is from Stack Overflow. Question asked by displaydisplayname

For example, suppose I have a multidimensional array:

const a1=[["a",["a","b"]],"b",[["b"],"c"]];

which the flatten from is

const a2=["a","a","b","b","c","c"];

and accessing a2[2] is equivalent to a1[0][1][1], also access a2[3] is equivalent to a1[1].

However I’m not interested in what the element is in such index, instead I want a function that can convert the index of a2 equivalent to in a1, eg:

magicFunction(2,[["a",["a","b"]],"b",[["b"],"c"]])

returns [0,1,1],

also

magicFunction(3,[["a",["a","b"]],"b",[["b"],"c"]])

returns [1]. And the input multidimensional array can be any depth (more than 3 layers). How do I write such magic function?

Note: I know that may be easy to do if I know the array is 2d and the sub-array have same number of elements:

const a1=[["a","a"],["b","b"],["c","c"]];
const magicFunction=function(target,a){
  let count=0;
  for(let i=0;i<a.length;i++){
    for(let j=0;j<a[i].length;j++){
        if(count==target){
            return [i,j];
        }
        count++;
    }
  }
  return [];
};
alert(magicFunction(3,a1));

However now I need the function to accept a with n-dimensional array which d is unknown before testing, which I don’t know how many layers of for-loop should I write to handle it, also the length of each sub-array may not be the same.



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?