Assume we have the following multidimensional array:
const myArray = [[[1, 2], 3], [4, 5], [6, 7], [7, 8], [9, 10]];
and I want to focus on element myArray[2][1], which is 7.
How can I find the single index of this element, despite the multidimensional array, in the fastest possible algorithm?
i.e. How can I find how many indivisible elements exist before this element?
So in the example above, the previous indivisible elements would be [0] = 1, [1] = 2, [2] = 3, [3] = 4, [4] = 5, [5] = 6 making the overall index of myArray[2][1], 6 .
Edit: Even though this example shows the largest dimension being 3-dimensions, is it possible to have an algorithm working for up to n-dimensions.
Ideally a function of the following form:
Array.prototype.getOverallIndex(i, j, k, ...)
{
...
return overallIndex;
}
[ [ [1, [ ] , [ [ [ [ ] ] ] ] , 2], 3], [4, 5], [6, 7], [7, 8], [9, 10]]6(the overall index of the input), then what is the input?[ 2, 1 ](the index path) or7(the value)? This would make a difference for repeated values, e.g. for[ [ 1, 2 ], [ 3, 2 ] ], I’d expect the overall index of the path[ 1, 1 ](representingarray[1][1]) to be3, but the overall index of the value2to be1(because it’s found atarray[0][1])..flat()method. EDIT It would also depend on your setup to adhere to these rules: i.imgur.com/ybBsdq1.png