-2

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?

2

1 Answer 1

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) );

Sign up to request clarification or add additional context in comments.

1 Comment

We tend not to answer poor questions that don't belong here in the first place as it just encourages more poor questions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.