JavaScript indexOf() method in an Object Array Last Updated : 14 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, indexOf() methods provide the first index at which the given element exists, and -1 in case it is not present in the array. Syntax: indexOf(element)indexOf(element, start);Parameters: element: the element to be searched in the input arraystart: the index from which the search has to begin. The default value is set to 0.Return value: JavaScript indexOf() method returns the value of the index at which the element is present, and -1 when the element doesn't exist. JavaScript indexOf() method in an Object Array ExamplesExample 1: Basic Usage In this example, the indexOf() method returns -1 because the object { name: 'banana', color: 'yellow' } is a different object instance than the one stored in the array. JavaScript let fruits = [ { name: 'apple', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'orange', color: 'orange' } ]; // Find the index of an object in the array let index = fruits.indexOf({ name: 'banana', color: 'yellow' }); console.log(index); // Output: -1 (Not found) Output-1 Example 2: Index of Modified Object In this case, the modified banana object is added to the array, and then indexOf() method returns the index where this modified object is located. JavaScript // Example Title: Finding Index of a Modified Object // Array of objects representing fruits let fruits = [ { name: 'apple', color: 'red' }, { name: 'banana', color: 'yellow' }, { name: 'orange', color: 'orange' } ]; let banana = { name: 'banana', color: 'yellow' }; // Finding the index of an object after modifying it fruits.push(banana); // Adding the modified object to the array let index = fruits.indexOf(banana); console.log(index); // Output: 3 Output3 Comment More infoAdvertise with us Next Article JavaScript indexOf() method in an Object Array P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array javascript-object JavaScript-Questions +1 More Similar Reads JavaScript Array indexOf() Method The indexOf() method in JavaScript is used to find the position of the first occurrence of a specific value in an array. If the value is not present, it returns -1. This method is handy for quickly determining where a particular item is located within an array.Syntax:array.indexOf(element, start)Par 3 min read JavaScript String indexOf() Method The indexOf() method in JavaScript is used to find the index of the first occurrence of a specified value within a string. The method returns a 0-based index, making it a fundamental JavaScript string manipulation way.The JavaScript indexOf() method helps locate a substring within a string and retur 3 min read JavaScript typedArray.indexOf() Method The typedArray.indexOf() is an inbuilt function in JavaScript which is used to return the index of the element if found in the given typedArray otherwise it returns -1. Syntax: typedarray.indexOf(Element, Index); Parameters: It accepts two parameter which are specified below- Element: It is the elem 2 min read TypeScript Array indexOf() Method The Array.indexOf() method in TypeScript is used to find the index of the first occurrence of a specified element in an array. If the element is not found, it returns -1.Syntaxarray.indexOf(searchElement[, fromIndex])Parameter: This method accepts two parameters as mentioned above and described belo 2 min read JavaScript TypedArray.prototype.findIndex() Method Method TypedArray.prototype.findIndex() of JavaScript returns the index of the first element in the typed array that fulfils the conditions specified by the testing function, if no elements satisfy it, -1 is returned, the findIndex() method takes a callback function as an argument and it returns an 2 min read JavaScript Get the index of an object by its property Given an object, the task is to get the object's index from the array of objects of the given property name and property value using JavaScript. we're going to discuss a few techniques. Below are the following approaches: Table of Content Using Array map() MethodUsing for loopUsing findIndex() Metho 3 min read TypeScript Array lastIndexOf() Method The Array.prototype.lastIndexOf() method in TypeScript is used to find the last index at which a given element can be found in the array, searching backwards from the fromIndex. It returns the index of the found element, or -1 if the element is not present.Syntax:array.lastIndexOf(searchElement[, fr 2 min read JavaScript Array lastIndexOf() Method The JavaScript Array lastIndexOf() Method is used to find the index of the last occurrence of the search element provided as the argument to the function.Syntax: array.lastIndexOf(element, start)Parameters: This method accepts two parameters as mentioned above and described below: element: This para 3 min read JavaScript Instanceof Operator The instanceof operator in JavaScript is used to check the type of an object at run time. It returns a boolean value if true then it indicates that the object is an instance of a particular class and if false then it is not. Syntax:let gfg = objectName instanceof objectTypeParameters: objectName: S 2 min read JavaScript typedArray.findIndex() with Example The Javascript typedArray.findIndex() is an inbuilt function in JavaScript that is used to return an index in the tyedArray if the value satisfies the condition given in the function, otherwise, it returns -1. Syntax: typedarray.findIndex(callback) Parameters: It takes the parameter âcallbackâ funct 2 min read Like