

The findIndex() method calls the callback function (the arrow function in our case) on each item in the array.

The for…in statement loops over the properties of an object, in our case the indices of the array. In this example we are going to use the for…in loop statement. With that being said, any looping statement can be used to accomplish this. When an object's property matches that of our target it will simply return the index. In a logical sense, to find the index of an object with a specific property, we could basically loop through each object starting from the first object with an index of 0 and keep comparing the property of the objects with our target property. So, if I had such array and I wanted to get the index of the object with an id of 3, how would I do that? Well, follow along! Method 1: A naive approach const arr = [ will return false because both are treated as distinct objects even if they have same property. That works quite fine, but what happens if it's an array of objects. If the items within the array are primitive data types, we can simply pass the value of the item to the indexOf() method to get the index of the array. One of such is the indexOf() method that returns the index of an item within the array. Arrays store data in a contiguous memory location and each data can be assessed by an index.Īs objects, Javascript arrays have many useful methods that help in manipulating the items in the array.

Although in a practical term, it is more likely to see an array as a collection of items of the same datatype.
