- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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
To Continue Learning Please Login
Login with Google