
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
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
- Related Questions & Answers
- JavaScript: Converting a CSV string file to a 2D array of objects
- How can I merge properties of two JavaScript objects dynamically?
- Find closest index of array in JavaScript
- Convert 2d tabular data entries into an array of objects in JavaScript
- How can I find documents in MongoDB based on the number of matched objects within an array?
- Find the dimensions of 2D array in Java
- How can I store JavaScript objects in cookies?
- Explain the Properties of 2D objects in JavaFX
- Manipulating objects in array of objects in JavaScript
- JavaScript - length of array objects
- Compare array of objects - JavaScript
- Array of objects to array of arrays in JavaScript
- Filtering array of objects in JavaScript
- Combine array of objects in JavaScript
- Turning a 2D array into a sparse array of arrays in JavaScript
Advertisements