javascript 通过 jquery 获取对象数组的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19111224/
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 14:23:27  来源:igfitidea点击:

Get index of array of objects via jquery

javascriptjqueryindexingutility

提问by FLuttenb

I have the following array:

我有以下数组:

var = array[
            {"id" : "aa", "description" : "some description"},
            {"id" : "bb", "description" : "some more description"},
            {"id" : "cc", "description" : "a lot of description"}]

and I try to find the index of the array that contains the id === "bb". The solution I came up with is the following:

我尝试找到包含id === "bb". 我想出的解决方案如下:

var i = 0;
while(array[i].id != "bb"){
   i++;
}
alert(i) //returns 1

Is there an easier way that has cross-browser functionality? I tried $.inArray(id,array)but it doesn't work.

有没有更简单的跨浏览器功能的方法?我试过了,$.inArray(id,array)但没有用。

回答by musefan

I don't see any problem with the complexity of your code, but I would recommend a couple of changes including adding some validation in case the value does not exists. Further more you can wrap it all in a reusable helper function...

我认为您的代码的复杂性没有任何问题,但我建议进行一些更改,包括添加一些验证,以防该值不存在。此外,您还可以将其全部包装在一个可重用的辅助函数中...

function getArrayIndexForKey(arr, key, val){
    for(var i = 0; i < arr.length; i++){
        if(arr[i][key] == val)
            return i;
    }
    return -1;
}

This can then be used in your example like so:

然后可以在您的示例中使用它,如下所示:

var index = getArrayIndexForKey(array, "id", "bb");
//index will be -1 if the "bb" is not found

Here is a working example

这是一个工作示例

NOTE: This should be cross browser compatible, and will also likely be faster than any JQuery alternative.

注意:这应该是跨浏览器兼容的,并且也可能比任何 JQuery 替代方案都快。

回答by Labu

var myArray = [your array];
var i = 0;

$.each(myArray, function(){
    if (this.id === 'bb') return false;
    i++;
})

console.log(i) // will log '1'


Update with modern JS.

使用现代 JS 更新。

let index
myArray.map(function(item, i){
    if (item.id === 'cc') index = i
})

console.log(index) // will log '2'

回答by Thirumalai murugan

inArray can't work with multidimensional array so try like the following

inArray 不能与多维数组一起使用所以尝试如下

var globalarray= [
            {"id" : "aa", "description" : "some description1"},
            {"id" : "bb", "description" : "some more description"},
            {"id" : "cc", "description" : "a lot of description"}];
var theIndex = -1;
for (var i = 0; i < globalarray.length; i++) {
    if (globalarray[i].id == 'bb') {
        theIndex = i;
        break;
    }
}
alert(theIndex);

Demo

演示

回答by Chokchai

You can use jQuery.each - http://api.jquery.com/jQuery.each/

您可以使用 jQuery.each - http://api.jquery.com/jQuery.each/

var i;
jQuery.each(array, function(index, value){
   if(value.id == 'bb'){
      i = index;
      return false; // retrun false to stop the loops
   }
});

回答by Andrii Skreka

Object.keys(yourObject).indexOf(yourValue);