0

I have an array of objects as such

const aaa = [
  {id: 10, val: "xx"},
  {id: 27, val: "tr"},
  {id: 13, val: "ut"}
]

Now, I know how to search for an object by a value of one of the properties. For example:

const bbb = aaa.find(obj => (obj.id === 27) )

The above code, bbb would equal to {id: 27, val: "tr"}

Now, what I want is to find the index of the object with id 27. In other words, for the above bbb object, the answer should be 1 (second item in the array)

How do I search for that.

Thank you.

3
  • 1
    You can use aaa.indexOf(bbb)? Commented Mar 8 at 2:53
  • @LãNgọcHải - Thank you. IS there another way? cause in reality, the objects in the array are very lart (lots of data), and indexOf might be too expensive
    – Greeso
    Commented Mar 8 at 2:55
  • 1
    no? you have the reference of the object, its index will be found quickly, I don't think it's gonna be expensive. Commented Mar 8 at 3:02

4 Answers 4

4

Use Array.prototype.findIndex()

const haystack = [
  {id: 10, val: "xx"},
  {id: 27, val: "tr"},
  {id: 13, val: "ut"}
]
const needleIndex = haystack.findIndex(obj => (obj.id === 27) );
console.log({ needleIndex });

4

Use the indexOf method with the object you are looking for.

let index = aaa.indexOf(bbb);

So full script would be...

const aaa = [
  {id: 10, val: "xx"},
  {id: 27, val: "tr"},
  {id: 13, val: "ut"}
]

const bbb = aaa.find(obj => (obj.id === 27) );

let index = aaa.indexOf(bbb);

console.log(index); 

2
  • 1
    This is unnecessarily inefficient, since it has to search twice. This is why findIndex() exists.
    – Barmar
    Commented Mar 8 at 5:18
  • @Barmar Thanks, I was unaware of the findIndex() function until now.
    – stuartb
    Commented Mar 9 at 7:36
1

const aaa = [
  {id: 10, val: "xx"},
  {id: 27, val: "tr"},
  {id: 13, val: "ut"}
]

const index = aaa.findIndex(item => item.id === 27)

console.log(index)

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Mar 8 at 8:52
1

Every array has a .findIndex() method, which takes a condition as an argument to search for the index of an array element. If the elements of the array are simple values, you can find the index of an element by providing a function that checks each element of the array.

let arr = ['a', 'b', 'c', 'd'];
let index = arr.findIndex(element => element === 'c'); // 2

Since your array contains objects, you need to pass a function to the .findIndex() method. This function will be executed for each element of the array (each object) during the search.

.findIndex(obj => obj.id === 27)

This expression checks, on each iteration of the search, whether the object has the key id with the value 27. If it does, the .findIndex() method will return the index of that element (object) in the array.

full code:

const aaa = [
  {id: 10, val: "xx"},
  {id: 27, val: "tr"},
  {id: 13, val: "ut"}
];

const index = aaa.findIndex(obj => obj.id === 27);

console.log(index);  // This will output: 1
2
  • "If the elements are simple values, you can just provide the value whose index you want to find" - No, .findIndex() doesn't work like that, the argument needs to be a function. Commented Mar 8 at 4:32
  • 1
    oh yeah, you're right. My bad. I haven't used it for a long time. Thanks for the correction.
    – Neriday
    Commented Mar 8 at 4:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.