indexOf method in an object array?

前端 未结 27 2476
别跟我提以往
别跟我提以往 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:31

    This is the way to find the object's index in array

        var myArray = [{  hello: 'world',
            foo: 'bar'
        },{
            hello: 'stevie',
            foo: 'baz'
        }];
    
    
    
        for (i = 0; i < myArray.length; i++) {
            if (myArray[i].hello === 'stevie') {
                alert('position: ' + i);
                return;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 02:32

    I like Pablo's answer, but Array#indexOf and Array#map don't work on all browsers. Underscore will use native code if it's available, but has fallbacks as well. Plus it has the pluck method for doing exactly what Pablo's anonymous map method does.

    var idx = _.chain(myArray).pluck("hello").indexOf("Stevie").value();
    
    0 讨论(0)
  • 2020-11-22 02:35

    Furthor of @Monika Garg answer, you can use findIndex() (There is a polyfill for unsupprted browsers).

    I saw that people downvoted this answer, and I hope that they did this because of the wrong syntax, because on my opinion, this is the most elegant way.

    The findIndex() method returns an index in the array, if an element in the array satisfies the provided testing function. Otherwise -1 is returned.

    For example:

    var hello = {
      hello: 'world',
      foo: 'bar'
    };
    var qaz = {
      hello: 'stevie',
      foo: 'baz'
    }
    
    var myArray = [];
    myArray.push(hello,qaz);
    
    var index = myArray.findIndex(function(element) {
      return element.hello == 'stevie';
    });
    
    alert(index);

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

    You can use a native and convenient function Array.prototype.findIndex() basically:

    The findIndex() method returns an index in the array, if an element in the array satisfies the provided testing function. Otherwise -1 is returned.

    Just a note it is not supported on Internet Explorer, Opera and Safari, but you can use a Polyfill provided in the link below.

    More information:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex

    var hello = {
      hello: 'world',
      foo: 'bar'
    };
    var qaz = {
      hello: 'stevie',
      foo: 'baz'
    }
    
    var myArray = [];
    myArray.push(hello, qaz);
    
    var index = myArray.findIndex(function(element, index, array) {
      if (element.hello === 'stevie') {
        return true;
      }
    });
    alert('stevie is at index: ' + index);

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

    I have made a generic function to check the below is the code & works for any object

    function indexOfExt(list, item) {
        var len = list.length;
    
        for (var i = 0; i < len; i++) {
            var keys = Object.keys(list[i]);
            var flg = true;
            for (var j = 0; j < keys.length; j++) {
                var value = list[i][keys[j]];
                if (item[keys[j]] !== value) {
                    flg = false;
                }
            }
            if (flg == true) {
                return i;
            }
        }
        return -1;
    }
    
    var items = [{ "hello": 'world', "foo": 'bar' }];
    var selectedItem = { "hello": 'world', "foo": 'bar' };
    alert(items.indexOf(selectedItem));
    alert(indexOfExt(items, selectedItem));
    

    The first alert will return -1 (means match not found) & second alert will return 0 (means match found).

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

    Use _.findIndex from underscore.js library

    Here's the example _.findIndex([{a:1},{a: 2,c:10},{a: 3}], {a:2,c:10}) //1

    0 讨论(0)
提交回复
热议问题