Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How can I find the index of a 2d array of objects in JavaScript?
To find the index of a specific object in a two-dimensional array, you need to search through both rows and columns. This involves using nested loops to traverse the matrix structure.
Syntax
function find2DIndex(matrix, searchCondition) {
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[row].length; col++) {
if (searchCondition(matrix[row][col])) {
return { row, col };
}
}
}
return null; // Not found
}
Example: Finding Object by Property
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 null;
}
const details = [
[
{studentName: 'John'}, {studentName:'David'}
],
[
{studentName:"Mike"},{studentName:'Bob'},{studentName:'Carol'}
]
];
var result = matrixIndexed(details, 'Bob');
console.log('Row:', result.r, 'Column:', result.c);
Row: 1 Column: 1
Using findIndex() Method
For a more functional approach, you can combine findIndex() methods:
function find2DIndexFunctional(matrix, searchValue, property) {
const rowIndex = matrix.findIndex(row =>
row.some(obj => obj[property] === searchValue)
);
if (rowIndex === -1) return null;
const colIndex = matrix[rowIndex].findIndex(obj =>
obj[property] === searchValue
);
return { row: rowIndex, col: colIndex };
}
const students = [
[{name: 'Alice', age: 20}, {name: 'Bob', age: 22}],
[{name: 'Charlie', age: 21}, {name: 'Diana', age: 23}]
];
const result = find2DIndexFunctional(students, 'Charlie', 'name');
console.log('Found at:', result);
Found at: { row: 1, col: 0 }
Handling Multiple Matches
To find all occurrences of a value in the 2D array:
function findAll2DIndexes(matrix, searchValue, property) {
const results = [];
for (let row = 0; row < matrix.length; row++) {
for (let col = 0; col < matrix[row].length; col++) {
if (matrix[row][col][property] === searchValue) {
results.push({ row, col });
}
}
}
return results;
}
const data = [
[{type: 'A', value: 1}, {type: 'B', value: 2}],
[{type: 'A', value: 3}, {type: 'C', value: 4}]
];
const allMatches = findAll2DIndexes(data, 'A', 'type');
console.log('All matches:', allMatches);
All matches: [ { row: 0, col: 0 }, { row: 1, col: 0 } ]
Comparison
| Method | Performance | Readability | Flexibility |
|---|---|---|---|
| Nested for loops | Fast | Good | High |
| findIndex() methods | Slower | Excellent | Medium |
| Custom search function | Fast | Excellent | Very High |
Conclusion
Use nested loops for simple searches in 2D arrays of objects. For more complex conditions or better readability, combine findIndex() methods or create custom search functions that accept callback conditions.
Advertisements
