在 Javascript 中查找多维数组的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16102263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 03:16:33  来源:igfitidea点击:

To find Index of Multidimensional Array in Javascript

javascript

提问by Rayon

I have created a multidimensional array in JavaScript and I want to find the exact index of specific value. That value will be user input.

我在 JavaScript 中创建了一个多维数组,我想找到特定值的确切索引。该值将是用户输入。

var array=[];
for(var i=0;i<10;i++)
{
    array[i] = [];
    for(var j=0;j<100;j++)
    {
        k = k +1
        array[i].push(k);
    }
}
var index=array.indexOf(**"What to insert here???"**);

回答by Du D.

JSFiddle

JSFiddle

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

// Generate Sample Data
var k = 0;
var array = [];
for (var i = 0; i < 10; i++) {
  array[i] = [];
  for (var j = 0; j < 100; j++) {
    k = k + 1;
    array[i].push(k);
  }
}
var needle = 130;
var result = getIndexOfK(array, needle);
console.log('The value #' + needle + ' is located at array[' + result[0] + '][' + result[1] + '].');

回答by Alberto Fecchi

this example seems to work well also with IRREGULAR multidimentional array:

这个例子似乎也适用于 IRREGULAR 多维数组:

function findIndex(valueToSearch, theArray, currentIndex) {
    if (currentIndex == undefined) currentIndex = '';
        if(Array.isArray(theArray)) {
            for (var i = 0; i < theArray.length; i++) {
                if(Array.isArray(theArray[i])) {
                    newIndex = findIndex(valueToSearch, theArray[i], currentIndex + i + ',');
                    if (newIndex) return newIndex;
               } else if (theArray[i] == valueToSearch) {
                   return currentIndex + i;
               }
            }
    } else if (theArray == valueToSearch) {
        return currentIndex + i;
    }
    return false;
}

var a = new Array();
a[0] = new Array(1, 2, 3, 4, 5);
a[1] = 'ciao';
a[2] = new Array(new Array(6,7),new Array(8,9),10);

var specificIndex = findIndex('10', a);

i wrote this speedly so everyone is invited to improve this function!

我写的很快,所以请大家改进这个功能!

p.s. now the function returns a STRING value with all indexes separated by comma, you can simply edit it to returns an object

ps 现在该函数返回一个字符串值,所有索引以逗号​​分隔,您可以简单地编辑它以返回一个对象

回答by Xotic750

On jsfiddle

jsfiddle 上

function indexOf2d(arr, val) {
    var index = [-1, -1];

    if (!Array.isArray(arr)) {
        return index;
    }

    arr.some(function (sub, posX) {
        if (!Array.isArray(sub)) {
            return false;
        }

        var posY = sub.indexOf(val);

        if (posY !== -1) {
            index[0] = posX;
            index[1] = posY;
            return true;
        }

        return false;
    });

    return index;
}

console.log(indexOf2d(array, 50));

回答by PedroProgrammer

My code does like a PROCV in MS Excel... and identifies the Index searching only in the first column. Maybe help you (or others).

我的代码确实像 MS Excel 中的 PROCV... 并且仅在第一列中标识索引搜索。也许可以帮助您(或其他人)。

var convertToRoman = function (valueLimitTen) {   
  var convertTable = [  [1, "I"],
                        [2, "II"],
                        [3, "III"],
                        [4, "IV"],
                        [5, "V"],
                        [6, "VI"],
                        [7, "VII"],
                        [8, "VIII"],
                        [9, "IV"],
                        [10, "X"],
                      ];

  var myIndex;  
  for(var i in convertTable){
    if(convertTable[i][0] == valueLimitTen){
      myIndex = i;      
      return convertTable[i][1];      
    }  
  }  
}

console.log(convertToRoman(2)); //Result II
console.log(convertToRoman(10)); //Result X