 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I find the index of a 2d array of objects in JavaScript?
To find the index of a two-dimensional array of objects, use two for loops, one for row and another for column. Following is the code −
Example
function matrixIndexed(details, name) {    var r;    var c;    for (r = 0; r < details.length; ++r) {       const nsDetails = details[r];       for (c = 0; c < nsDetails.length; ++c) {          const tempObject = nsDetails[c];          if (tempObject.studentName === name) {             return { r, c};          }       }    }    return {}; } const details = [    [       {studentName: 'John'}, {studentName:'David'}    ],    [       {studentName:"Mike"},{studentName:'Bob'},{studentName:'Carol'}    ] ]; var {r, c } = matrixIndexed(details, 'Bob'); console.log(r, c); To run the above program, you need to use the following command −
node fileName.js.
Output
Here, my file name is demo160.js. This will produce the following output −
PS C:\Users\Amit\JavaScript-code> node demo160.js 1 1
Advertisements
 