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 to create a multidimensional JavaScript object?
A multidimensional JavaScript object is an object that contains other objects as properties, creating nested structures. This allows you to organize complex data hierarchically.
Basic Syntax
let multidimensionalObject = {
property1: "value1",
property2: {
nestedProperty1: "nestedValue1",
nestedProperty2: {
deeplyNestedProperty: "deeplyNestedValue"
}
}
};
Example: Creating a Student Object
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multidimensional Object</title>
</head>
<body>
<button onclick="createObject()">Create Object</button>
<script>
function createObject() {
let student = {
firstName: "John",
lastName: "Doe",
age: 20,
address: {
street: "123 Main St",
city: "New York",
state: "NY",
coordinates: {
latitude: 40.7128,
longitude: -74.0060
}
},
courses: {
math: {
grade: "A",
credits: 3
},
science: {
grade: "B+",
credits: 4
}
}
};
document.getElementById("output").innerHTML =
"<h3>Student Information:</h3>" +
"<p><strong>Name:</strong> " + student.firstName + " " + student.lastName + "</p>" +
"<p><strong>City:</strong> " + student.address.city + "</p>" +
"<p><strong>Math Grade:</strong> " + student.courses.math.grade + "</p>" +
"<p><strong>Coordinates:</strong> " + student.address.coordinates.latitude + ", " + student.address.coordinates.longitude + "</p>";
}
</script>
</body>
</html>
Accessing Nested Properties
let company = {
name: "TechCorp",
employees: {
engineering: {
count: 50,
manager: "Alice Smith"
},
sales: {
count: 30,
manager: "Bob Johnson"
}
}
};
// Accessing nested properties using dot notation
console.log(company.name); // "TechCorp"
console.log(company.employees.engineering.count); // 50
console.log(company.employees.sales.manager); // "Bob Johnson"
// Accessing using bracket notation
console.log(company["employees"]["engineering"]["manager"]); // "Alice Smith"
TechCorp 50 Bob Johnson Alice Smith
Dynamic Property Creation
let inventory = {};
// Adding nested properties dynamically
inventory.electronics = {};
inventory.electronics.laptops = {
count: 25,
brand: "Dell"
};
inventory.electronics.phones = {
count: 40,
brand: "Samsung"
};
console.log(inventory.electronics.laptops.count); // 25
console.log(inventory.electronics.phones.brand); // "Samsung"
25 Samsung
Common Use Cases
| Use Case | Example Structure |
|---|---|
| User Profiles | user.profile.personal.address |
| Configuration Settings | config.database.connection.host |
| API Responses | response.data.results.items |
| Game Data | game.player.stats.level |
Best Practices
- Use meaningful property names for better readability
- Consider using optional chaining (?.) for safe property access
- Keep nesting levels reasonable to avoid overly complex structures
- Use bracket notation when property names contain special characters
Conclusion
Multidimensional JavaScript objects provide a powerful way to organize complex data structures. They're essential for handling real-world data like user profiles, configuration settings, and API responses with nested relationships.
Advertisements
