How to find index of array of object given a property value?

I have an array of object that holds two objects. Each object has a couple properties; one being dealerNo. Given the value of dealerNo, how would I get the index of that object?

1 thought on “How to find index of array of object given a property value?”

  1. Use .findIndex:

    const getIndexByDealerNo = (arr=[], dealerNo) =>
      arr.findIndex(e => e.dealerNo===dealerNo);
      
    const arr = [ { dealerNo:1 }, { dealerNo: 2 } ];
    
    console.log( getIndexByDealerNo(arr, 1) );
    console.log( getIndexByDealerNo(arr, 2) );
    console.log( getIndexByDealerNo(arr, 11) );
    Reply

Leave a Comment