简体   繁体   English

如何找到三维数组的索引

[英]How to find index of a three dimensional array

I saw this post: To find Index of Multidimensional Array in Javascript 我看到了这篇文章: 在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];
        }
     }
   }
}

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 [];
}

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. 您的输出必须是3个整数的数组,就像您正确完成的一样。

/**
* 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/ 试试看https://jsfiddle.net/ygo7o6w1/

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] 

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)) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM