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?
Asked
Active
Viewed 48 times
-2

ProgrammingNoob221
- 13
- 1
-
Always include the code that you are asking about. – Scott Marcus Feb 15 '21 at 21:41
-
[How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/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 Feb 15 '21 at 21:44
1 Answers
0
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) );

Majed Badawi
- 27,616
- 4
- 25
- 48
-
We tend not to answer poor questions that don't belong here in the first place as it just encourages more poor questions. – Scott Marcus Feb 15 '21 at 22:20