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?
-
Always include the code that you are asking about.Scott Marcus– Scott Marcus2021-02-15 21:41:11 +00:00Commented Feb 15, 2021 at 21:41
-
How much research effort is expected of Stack Overflow users? "Asking a question on Stack Overflow should be the last step in your process for finding an answer"Thomas Sablik– Thomas Sablik2021-02-15 21:44:24 +00:00Commented Feb 15, 2021 at 21:44
Add a comment
|
1 Answer
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) );
1 Comment
Scott Marcus
We tend not to answer poor questions that don't belong here in the first place as it just encourages more poor questions.