Introduction:
Learn the essentials of working with JavaScript objects and JSON data. Understand the fundamentals of creating, manipulating, and accessing properties of objects in JavaScript. Dive into the power of JSON for data interchange and explore the nuances of object property access methods.
Here’s a brief explanation of JSON object literals and how they relate to JavaScript object literals:
Example of a JSON object literal:
{ "name": "Omar Abobakr", "age": 20, "city": "Cairo" }
Example of a JavaScript object literal:
var person = { name: "Omar Abobakr", age: 20, city: "Cairo" };
complete code in html with explanation
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>JSON Object Literal Example</title> </head> <body> <h1>JSON Object Literal Example</h1> <script> // JavaScript Object Literal var person = { name: "Omar Abobakr", age: 20, city: "Cairo" }; // Accessing properties of the object var personName = person.name; var personAge = person.age; var personCity = person.city; // Displaying information in HTML document.write("<p><strong>Name:</strong> " + personName + "</p>"); document.write("<p><strong>Age:</strong> " + personAge + "</p>"); document.write("<p><strong>City:</strong> " + personCity + "</p>"); </script> </body> </html>
Explanation:
When you open this HTML file in a web browser, you should see a page displaying information about the person defined in the JavaScript object literal.
How to create JavaScript Objects from JSON object
Here’s an example of how to create JavaScript objects from a JSON object:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>JSON to JavaScript Object Example</title> </head> <body> <h1>JSON to JavaScript Object Example</h1> <script> // JSON object as a string var jsonStr = '{"name": "Omar Abobakr", "age": 20, "city": "Cairo"}'; // Parse the JSON string to create a JavaScript object var person = JSON.parse(jsonStr); // Accessing properties of the JavaScript object var personName = person.name; var personAge = person.age; var personCity = person.city; // Displaying information in HTML document.write("<p><strong>Name:</strong> " + personName + "</p>"); document.write("<p><strong>Age:</strong> " + personAge + "</p>"); document.write("<p><strong>City:</strong> " + personCity + "</p>"); </script> </body> </html>
Explanation:
Here’s a step-by-step example:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Parse JSON String to JavaScript Object</title> </head> <body> <h1>Parse JSON String to JavaScript Object</h1> <script> // JSON string representing a person's information var jsonString = '{"name": "Omar Abobakr", "age": 20, "city": "Cairo"}'; // Parse the JSON string to create a JavaScript object var personObject = JSON.parse(jsonString); // Displaying information in HTML document.write("<p><strong>Name:</strong> " + personObject.name + "</p>"); document.write("<p><strong>Age:</strong> " + personObject.age + "</p>"); document.write("<p><strong>City:</strong> " + personObject.city + "</p>"); </script> </body> </html>
Explanation:
In JavaScript, you can access object values using dot notation. Dot notation is a simple and common way to retrieve the values of properties from an object.
Here’s an example:
// Define an object var person = { name: "Omar Abobakr", age: 20, city: "Cairo" }; // Access object values using dot notation var personName = person.name; var personAge = person.age; var personCity = person.city; // Display the values console.log("Name: " + personName); console.log("Age: " + personAge); console.log("City: " + personCity);
In this example:
Here’s how you might use dot notation in an HTML document to display object values:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Access Object Values Using Dot Notation</title> </head> <body> <h1>Access Object Values Using Dot Notation</h1> <script> // Define an object var person = { name: "Omar Abobakr", age: 20, city: "Cairo" }; // Access object values using dot notation var personName = person.name; var personAge = person.age; var personCity = person.city; // Display the values in HTML document.write("<p><strong>Name:</strong> " + personName + "</p>"); document.write("<p><strong>Age:</strong> " + personAge + "</p>"); document.write("<p><strong>City:</strong> " + personCity + "</p>"); </script> </body> </html>
This HTML document uses JavaScript to access object values using dot notation and then displays them in HTML paragraphs.
In JavaScript, you can access object values using bracket notation, which is particularly useful when the property names are dynamic, stored in variables, or contain special characters.
Here’s an example:
// Define an object var person = { name: "Omar Abobakr", age: 20, city: "Cairo" }; // Access object values using bracket notation var propertyName = "name"; var personName = person[propertyName]; // Display the value console.log("Name: " + personName);
In this example:
Here’s how you might use bracket notation in an HTML document to display object values:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Access Object Values Using Bracket Notation</title> </head> <body> <h1>Access Object Values Using Bracket Notation</h1> <script> // Define an object var person = { name: "Omar Abobakr", age: 20, city: "Cairo" }; // Access object values using bracket notation var propertyName = "name"; var personName = person[propertyName]; // Display the value in HTML document.write("<p><strong>Name:</strong> " + personName + "</p>"); </script> </body> </html>
This HTML document uses JavaScript to access object values using bracket notation and then displays the value in an HTML paragraph. The flexibility of bracket notation makes it a powerful choice when dealing with dynamic property names.
Here’s an example:
// Define an object var person = { name: "Omar Abobakr", age: 20, city: "Cairo" }; // Loop through object properties using for...in loop for (var key in person) { // Check if the property is directly on the object (not inherited) if (person.hasOwnProperty(key)) { console.log(key + ": " + person[key]); } }
In this example:
Here’s how you might use a for…in loop in an HTML document to display object properties:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Loop Through Object Properties</title> </head> <body> <h1>Loop Through Object Properties</h1> <script> // Define an object var person = { name: "Omar Abobakr", age: 20, city: "Cairo" }; // Loop through object properties using for...in loop for (var key in person) { // Check if the property is directly on the object (not inherited) if (person.hasOwnProperty(key)) { document.write("<p><strong>" + key + ":</strong> " + person[key] + "</p>"); } } </script> </body> </html>
This HTML document uses a for…in loop in JavaScript to iterate over the properties of the person object and display them in HTML paragraphs.
In a for…in loop, you can use bracket notation to access the property values of an object.
Here’s an example:
// Define an object var person = { name: "Omar Abobakr", age: 20, city: "Cairo" }; // Loop through object properties using for...in loop for (var key in person) { // Check if the property is directly on the object (not inherited) if (person.hasOwnProperty(key)) { // Use bracket notation to access property values var propertyValue = person[key]; console.log(key + ": " + propertyValue); } }
In this example:
Here’s how you might use this technique in an HTML document to display object properties:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Access Object Values in for...in Loop</title> </head> <body> <h1>Access Object Values in for...in Loop</h1> <script> // Define an object var person = { name: "Omar Abobakr", age: 20, city: "Cairo" }; // Loop through object properties using for...in loop for (var key in person) { // Check if the property is directly on the object (not inherited) if (person.hasOwnProperty(key)) { // Use bracket notation to access property values var propertyValue = person[key]; document.write("<p><strong>" + key + ":</strong> " + propertyValue + "</p>"); } } </script> </body> </html>
This HTML document uses a for…in loop in JavaScript to iterate over the properties of the person object and display them in HTML paragraphs, utilizing bracket notation for property access.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Person Information App</title> <style> table { border-collapse: collapse; width: 100%; margin-top: 20px; } th, td { border: 1px solid #dddddd; text-align: left; padding: 8px; } th { background-color: #f2f2f2; } </style> </head> <body> <h1>Person Information App</h1> <script> // Array of person objects var people = [ { name: "Omar Abobakr", age: 20, city: "Cairo" }, { name: "Jane Smith", age: 25, city: "Los Angeles" }, { name: "Bob Johnson", age: 35, city: "Chicago" } ]; // Display person information in an HTML table document.write("<table>"); document.write("<tr><th>Name</th><th>Age</th><th>City</th></tr>"); for (var i = 0; i < people.length; i++) { document.write("<tr>"); for (var key in people[i]) { if (people[i].hasOwnProperty(key)) { // Use bracket notation to access property values var propertyValue = people[i][key]; document.write("<td>" + propertyValue + "</td>"); } } document.write("</tr>"); } document.write("</table>"); </script> </body> </html>
Explanation:
This example demonstrates how you can use a for…in loop and bracket notation to dynamically access and display information from an array of objects in an HTML table.
Here’s a quiz with 15 questions related to the lesson on JavaScript objects, JSON, and object property access. Each question has multiple-choice answers. Feel free to use this for testing your knowledge.
a. JavaScript Object Notation
b. Java Standard Object Notation
c. JavaScript Object Naming
d. Java Serialized Object Notation
a. stringify()
b. parse()
c. convert()
d. decode()
a. “key”: “value”
b. key = value
c. {key: value}
d. [key, value]
a. object.property
b. object[“property”]
c. object->property
d. object::property
a. To check if an object has any properties
b. To check if a property exists in an object
c. To remove a property from an object
d. To add a new property to an object
a. <paragraph>
b. <div>
c. <span>
d. <output>
a. To iterate over inherited properties
b. To prevent the loop from executing
c. To check if the object is empty
d. To iterate only over object’s own properties
a. To create a new property in an object
b. To delete a property from an object
c. To access a property using its name as a string
d. To access a property using its value
a. {name: “John”, age: 25}
b. {“name”: “John”, “age”: 25}
c. name: “John”, age: 25
d. [name: “John”, age: 25]
a. object = new Object();
b. Object.create();
c. var obj = {}
d. new Object[];
a. To convert a JavaScript object to a JSON string
b. To parse a JSON string into a JavaScript object
c. To access object properties
d. To check if a property exists in an object
a. JSON is a programming language
b. JSON is a data interchange format
c. JSON is an acronym for “Java Serialized Object Notation”
d. JSON is only used for client-side scripting
a. An error
b. 42
c. “42”
d. {“42”: true}
a. object.add(“newProperty”, value);
b. object[“newProperty”] = value;
c. object.addProperty(“newProperty”, value);
d. object.newProperty = value;
a. To define HTML structure
b. To style web pages
c. To handle server-side logic
d. To exchange data between a server and a web application
1-a
2-b
3-a
4-a
5-b
6-b
7-d
8-c
9-b
10-c
11-a
12-b
13-b
14-b
15-d