indexOf method in an object array?

前端 未结 27 2411
别跟我提以往
别跟我提以往 2020-11-22 02:18

What\'s the best method to get the index of an array which contains objects?

Imagine this scenario:

var hello = {
    hello: \'world\',
    foo: \'ba         


        
相关标签:
27条回答
  • 2020-11-22 02:44

    If you are only interested into finding the position see @Pablo's answer.

    pos = myArray.map(function(e) { return e.hello; }).indexOf('stevie');

    However, if you are looking forward to finding the element (i.e. if you were thinking of doing something like this myArray[pos]), there is a more efficient one-line way to do it, using filter.

    element = myArray.filter((e) => e.hello === 'stevie')[0];

    See perfomance results (~ +42% ops/sec): http://jsbench.github.io/#7fa01f89a5dc5cc3bee79abfde80cdb3

    0 讨论(0)
  • 2020-11-22 02:45

    See this example: http://jsfiddle.net/89C54/

    for (i = 0; i < myArray.length; i++) {
        if (myArray[i].hello === 'stevie') {
            alert('position: ' + i);
            return;
        }
    }
    

    It starts to count with zero.

    0 讨论(0)
  • 2020-11-22 02:47

    I think you can solve it in one line using the map function:

    pos = myArray.map(function(e) { return e.hello; }).indexOf('stevie');
    
    0 讨论(0)
  • 2020-11-22 02:50

    While, most other answers here are valid. Sometimes, it's best to just make a short simple function near where you will use it.

    // indexOf wrapper for the list of objects
    function indexOfbyKey(obj_list, key, value) {
        for (index in obj_list) {
            if (obj_list[index][key] === value) return index;
        }
        return -1;
    }
    // Find the string in the list (default -1)
    var test1 = indexOfbyKey(object_list, 'name', 'Stevie');
    var test2 = indexOfbyKey(object_list, 'last_name', 'some other name');
    

    It depends on what is important to you. It might save lines of code and be very clever to use a one-liner, or to put a generic solution somewhere that covers various edge cases. But sometimes it's better to just say: "here I did it like this" rather than leave future developers to have extra reverse engineering work. Especially if you consider yourself "a newbie" like in your question.

    0 讨论(0)
  • 2020-11-22 02:51

    If your object is the same object of the ones you are using within the array, you should be able to get the index of the Object in the same way you do as if it was a string.

    var hello = {
        hello: 'world',
        foo: 'bar'
    };
    var qaz = {
        hello: 'stevie',
        foo: 'baz'
    }
    
    var qazCLONE = { // new object instance and same structure
        hello: 'stevie',
        foo: 'baz'
    }
    
    var myArray = [hello,qaz];
    
    myArray.indexOf(qaz) // should return 1
    myArray.indexOf(qazCLONE) // should return -1
    
    0 讨论(0)
  • 2020-11-22 02:52
    var hello = {hello: "world",  foo: "bar"};
    var qaz = {hello: "stevie", foo: "baz"};
    var myArray = [];
    myArray.push(hello,qaz);
    
    function indexOfObject( arr, key, value   ) {
        var j = -1;
        var result = arr.some(function(obj, i) { 
            j++;
            return obj[key] == value;
        })
    
        if (!result) {
            return -1;
        } else {
            return j;
        };
    }
    
    alert(indexOfObject(myArray,"hello","world"));
    
    0 讨论(0)
提交回复
热议问题