Objects
A JavaScript Object is a container that holds the properties of the object in a key-value pair. The properties of an object are accessed using dot notation or square brackets notation. Objects that contain objects inside them are called nested or cascaded Objects. Accessing the value in nested objects can be done by consecutively applying dot or square bracket notation.
Approach 1: Accessing properties using dot notation
Syntax:
objectName.property1Name.propert2Name...
The dot notation method checks for the property name inside the object and if it exists returns the value associated with that property inside the object. In case the returned value is an object, the sub-properties of that object can be accessed again using the dot notation till we reach the required property value.
Example: Consider an Object named person having properties name and contact where contact is an object consisting of properties phone, email, and address where address is an object consisting of properties namely city and country. To access the city property using dot notation can be done as person.contact.address.city
let person = {
name: "John",
contact: {
phone: "987-654-3210",
email: "john123@xyz.com",
address: {
city: "Berlin",
country: "Germany"
}
}
};
console.log(person.contact.email);
console.log(person.contact.address.city);
Output
john123@xyz.com Berlin
Approach 2: Accessing properties using square bracket notation
Syntax:
objectName[property1Name][propertyName2]...
The square bracket notation method checks for the property name inside the object by comparing the string value provided in square bracket notation to the properties of the object and if it exists, returns the value associated with that property inside the object. In case the returned value is an object, the sub-properties of that object can be accessed again in the same way till we reach the required property value.
Example: Consider an Object named person having properties name and contact where contact is an object consisting of properties phone, email, and address where address is an object consisting of properties namely city and country. To access the city property using square bracket notation can be done as a person[“contact”][“address”][“city”].
let person = {
name: "Shaun",
contact: {
phone: "934-379-1420",
email: "shaun2000@abc.com",
address: {
city: "London",
country: "United Kingdom"
}
}
};
console.log(person["contact"]["email"]);
console.log(person["contact"]["address"]["country"]);
Output
shaun2000@abc.com United Kingdom
How to access and process nested objects, arrays, or JSON ?
JavaScript is a scripting and programming language used to add dynamic features to web pages, making them more than just static displays. Often, we need data structures like Objects, Arrays, or JavaScript Object Notation (JSON) to temporarily store data, whether it’s hardcoded or fetched from a database. As data complexity grows, we might organize it into nested structures—like putting one container inside another—for easier access and retrieval.
In this article, we will explore methods for accessing nested JavaScript Objects, Arrays, or JSON. Whether you’re dealing with layers of data, we will show you how to navigate through them effectively.
Contact Us