Javascript在数组中搜索值并获取其密钥

时间:2022-02-18 23:45:14

I want to make a function that works like this:

我想创建一个像这样工作的函数:

function arraySearch(array, valuetosearchfor)
{
// some code
}

if it finds the value in the array, it will return the key, where it found the value. If there is more than one result (more than one key), or no results at all (nothing found), then the function will return FALSE.

如果它在数组中找到值,它将返回键,在那里找到值。如果有多个结果(多个键),或者根本没有结果(找不到任何结果),则该函数将返回FALSE。

I found this code:

我找到了这段代码:

function arraySearch(arr,val) 
        {
            for (var i=0; i<arr.length; i++)
                {
                    if (arr[i] == val) 
                    {                    
                        return i;
                    }
                    else 
                    {
                        return false;
                    }
                }
        }

and used it like this:

并像这样使用它:

var resultofarraycheck = arraySearch(board, chosen);
                if (resultofarraycheck === false) 
                {
                       document.getElementById(buttonid).value;
                       chosen = 0;
                }

But it doesn't seem to work. When it should find something, it returns false instead of the key (i).

但它似乎没有用。当它应该找到某些东西时,它返回false而不是键(i)。

How can I fix this, or what am I doing wrong?

我该如何解决这个问题,或者我做错了什么?

Thanks, and I'm sorry if my English wasn't clear enough.

谢谢,如果我的英语不够清楚,我很抱歉。

4 个解决方案

#1


18  

function arraySearch(arr,val) {
    for (var i=0; i<arr.length; i++)
        if (arr[i] === val)                    
            return i;
    return false;
  }

#2


12  

You can use indexOf to get key jsfiddle

您可以使用indexOf来获取关键的jsfiddle

if(!Array.prototype.indexOf){
    Array.prototype.indexOf = function(val){
        var i = this.length;
        while (i--) {
            if (this[i] == val) return i;
        }
        return -1;
    } 
}

    var arr = ['a','b','c','d','e'];

    var index = arr.indexOf('d'); // return 3

#3


2  

function arraySearch(arr, val) {
  var index;
  for (var i = 0; i < arr.length; i++) {
    // use '===' if you strictly want to find the same type
    if (arr[i] == val) {
      if (index == undefined) index = i;
      // return false if duplicate is found
      else return false;
    }
  }

  // return false if no element found, or index of the element
  return index == undefined ? false : index;
}

Hope this helps :)

希望这可以帮助 :)

#4


0  

This is a method on the Array Prototype now: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

现在这是Array Prototype上的一个方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

var array1 = [5, 12, 8, 130, 44];

function findFirstLargeNumber(element) {
  return element > 13;
}

console.log(array1.findIndex(findFirstLargeNumber));
// expected output: 3

#1


18  

function arraySearch(arr,val) {
    for (var i=0; i<arr.length; i++)
        if (arr[i] === val)                    
            return i;
    return false;
  }

#2


12  

You can use indexOf to get key jsfiddle

您可以使用indexOf来获取关键的jsfiddle

if(!Array.prototype.indexOf){
    Array.prototype.indexOf = function(val){
        var i = this.length;
        while (i--) {
            if (this[i] == val) return i;
        }
        return -1;
    } 
}

    var arr = ['a','b','c','d','e'];

    var index = arr.indexOf('d'); // return 3

#3


2  

function arraySearch(arr, val) {
  var index;
  for (var i = 0; i < arr.length; i++) {
    // use '===' if you strictly want to find the same type
    if (arr[i] == val) {
      if (index == undefined) index = i;
      // return false if duplicate is found
      else return false;
    }
  }

  // return false if no element found, or index of the element
  return index == undefined ? false : index;
}

Hope this helps :)

希望这可以帮助 :)

#4


0  

This is a method on the Array Prototype now: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

现在这是Array Prototype上的一个方法:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

var array1 = [5, 12, 8, 130, 44];

function findFirstLargeNumber(element) {
  return element > 13;
}

console.log(array1.findIndex(findFirstLargeNumber));
// expected output: 3