h3ky1
h3ky1

Reputation: 61

How to find index of a three dimensional array

I saw this post: To find Index of Multidimensional Array in Javascript

I liked the answer but I am trying to extend it to work for three dimensional arrays. This is what I have so far. Any help would be appreciated.

/**
* Index of Multidimensional Array
* @param a1,a2 {!Array} - the input arrays
* @param k {object} - the value to search
* @return {Array} 
*/
function getIndexOfK(a1, a2, k) {
  for (var i = 0; i < arr.length; i++) {
    for (j=0; j<a2.length;j++){
        var index = arr[i][j].indexOf(k); 
        if (index > -1) {
            return [i, j, index];
        }
     }
   }
}

Upvotes: 0

Views: 1401

Answers (4)

T4rk1n
T4rk1n

Reputation: 1470

Try this functional approach:

const mda = [
  [
    [1, 2],
    [3, 4]
  ],
  [
    [5, 6],
    [7, 8]
  ],
  [
    [9, 10],
    [11, 12]
  ]
]

const findIt = (arr, t) => arr.reduce((a, e, i) => a || e.reduce((a1, e1, j) => {
  if (a1) return a1
  const index = e1.indexOf(t)
  if (index >= 0) return [i, j, index]
}, null), null)

console.log(findIt(mda, 10))

Upvotes: 0

rgthree
rgthree

Reputation: 7273

Why stop at three dimensions? I'd create a recursive function that accepts only one array of any dimenions and recursively calls itself finding the path to the most shallow value to find.

function getIndexPathOf(arr, k) {
  // If we're not array, return null;
  if (!Array.isArray(arr))
    return null;
  
  // If our item is directly within our current
  // array, return it's index as an array.
  var shallowIndex = arr.indexOf(k);
  if (shallowIndex > -1)
    return [shallowIndex];
  
  // Search through our current array, recursively
  // calling our getIndexPathOf with the current value.
  for (var i = 0, l = arr.length; i < l; i++) {
    var path = getIndexPathOf(arr[i], k);
    if (path != null) {
      // If we found the path, prepend the current index
      // and return.
      path.unshift(i);
      return path;
    }
  }
  
  // If nothing was found, return null.
  return null;
}


console.log(getIndexPathOf([1, 2, 3], 3)); // [2]
console.log(getIndexPathOf([1, 2, 3], 4)); // null
console.log(getIndexPathOf([1, 2, ['A', [0, 10, 20, [3, 6]], 'B'], 4, 5], 3)); // [2, 1, 3, 0]
console.log(getIndexPathOf([1,[2],[[3]],[[[4]]],[[[[5]]]]], 5)); // [4, 0, 0, 0, 0]

Upvotes: 0

PotatoManager
PotatoManager

Reputation: 1775

A three dimensional array is essentially a single array. Your output will have to be an array of 3 integers like you've correctly done.

/**
* Index of Multidimensional Array
* @param array - the input 3-D array
* @param x {object} - the value to search
* @return {Array} 
*/
function getIndexOf(array, x) {
 for (var i = 0; i < array.length; i++) {
  for (j=0; j<array[0].length;j++){
  var k = array[i][j].indexOf(x); 
  if (k > -1) {
    return [i, j, k];
  }
 }
}

}

Try it out https://jsfiddle.net/ygo7o6w1/

Upvotes: 0

Hugo sama
Hugo sama

Reputation: 909

Modified Fiddle

You dont need a second array on the function parameters, you just look deeper into the third dimension like so :

function getIndexOfK(arr, k){
    if (!arr){
        return [];
    }

for(var i=0; i<arr.length; i++){
    for( var j = 0 ; j < arr[i].length; j ++ ) {
        var index = arr[i][j].indexOf(k);
        if (index > -1){
            return [i, j,index];
        }        
  }
}

    return [];
}

Upvotes: 1

Related Questions