JavaScript • Data Structures

Objects, Arrays of Objects & Objects of Arrays

A student-friendly guide to storing related information using named properties and combining objects with arrays.

3.1 What is an Object?

An object is a collection of named properties. Each property has a key (property name) and a value.

Key idea: Arrays normally use numeric indexes such as 0, 1, and 2. Objects use meaningful names such as name, age, and isStudent.
let person = {
  name: "Alice",
  age: 25,
  isStudent: true
};
KeyValue
name"Alice"
age25
isStudenttrue

3.2 Object Literal Syntax

You create an object using curly braces { }. Inside them, properties are written as key : value pairs separated by commas.

Example 7 — Basic object
let person = {
  name: "Alice",      // line 2
  age: 25,            // line 3
  isStudent: true     // line 4
};

console.log(person.name); // line 7
console.log(person.age);  // line 8

How the example works

  1. let person = { starts an object literal assigned to person.
  2. name: "Alice" creates a property named name with the value "Alice".
  3. age: 25 creates an age property with a numeric value.
  4. isStudent: true creates an isStudent property with a Boolean value.
  5. } ends the object literal.
  6. person.name uses dot notation to access the name property.
  7. person.age uses dot notation to access the age property.

3.3 Accessing and Modifying Properties

JavaScript provides two common ways to access object properties:

MethodSyntaxExample
Dot notationobject.propertyNamecar.brand
Bracket notationobject["propertyName"]car["model"]

You can also add new properties or change existing properties after the object has been created.

Example 8 — Dot vs bracket notation
let car = {
  brand: "Toyota",
  model: "Corolla"
};

console.log(car.brand);        // line 5
console.log(car["model"]);     // line 6

car.year = 2020;               // line 8
car["color"] = "blue";         // line 9

console.log(car.year);         // line 11
console.log(car.color);        // line 12
Remember: Dot notation is convenient when the property name is a normal identifier. Bracket notation explicitly places the property name inside quotes and is useful when working with a property name dynamically.

What happens here?

  • car.brand accesses brand using dot notation.
  • car["model"] accesses model using bracket notation.
  • car.year = 2020 adds a new year property.
  • car["color"] = "blue" adds a new color property.

3.4 Objects Containing Multiple Types

Object property values can be different JavaScript data types. The source material highlights strings, numbers, booleans, arrays, other objects, and functions (methods).

Example 9 — Object with various value types
let book = {
  title: "JavaScript Guide",
  pages: 300,
  available: true
};

console.log(book.title);     // line 6
console.log(book.pages);     // line 7
console.log(book.available); // line 8

Here, title stores a string, pages stores a number, and available stores a Boolean value.

4. Arrays of Objects and Objects of Arrays

Real-world data often requires combining arrays and objects. This lets us represent collections of similar records or group related lists under named keys.

Think of it this way: an array of objects is useful for a list of similar entities, while an object of arrays groups several related lists under meaningful property names.

4.1 Array of Objects

An array of objects is useful when you have a list of similar entities, each with named fields.

Example 10 — Array of objects
let students = [
  { name: "Alice", age: 20 },  // line 2
  { name: "Bob", age: 22 },    // line 3
  { name: "Cara", age: 21 }     // line 4
];

console.log(students[0].name); // line 7
console.log(students[1].age);  // line 8

Understanding the access pattern

  1. students is an array.
  2. Each { ... } is an object representing one student.
  3. students[0].name gets the first object and then reads its name.
  4. students[1].age gets the second object and then reads its age.
Example 11 — Iterating over an array of objects
let students = [
  { name: "Alice", age: 20 },
  { name: "Bob", age: 22 },
  { name: "Cara", age: 21 }
];

for (let i = 0; i < students.length-1; i++) {
  console.log(students[i].name + " is " + students[i].age);
}

The loop accesses each student object through students[i], then reads its name and age properties.

Another example — for...of with for...in
let students = [
  {name:'bob', branch:"CSE-AIML", fee:350000},
  {name:'ben', branch:"CSE", fee:300000},
  {name:'bint', branch:"CSE-DS", fee:300000}
];

for(let value of students){
  for(let key in value){
    console.log(key+" is "+value[key]);
  }
  console.log("----------------");
}
Reading the nested loops: for...of gets each student object from the array. Inside it, for...in visits the keys of that object, and value[key] reads the corresponding value.

4.2 Object of Arrays

An object of arrays is useful when you group several related lists under named keys.

let course = {
  titles: ["Math", "Physics", "Chemistry"],   // line 2
  credits: [3, 4, 3]                          // line 3
};

console.log(course.titles[1]);   // line 6
console.log(course.credits[1]);  // line 7

How it works

  • course is an object with two properties.
  • titles is an array containing course names.
  • credits is an array containing credit values.
  • course.titles[1] accesses "Physics".
  • course.credits[1] accesses 4, the credits for Physics.

Quick Review

ConceptWhat it representsExample access
ObjectNamed propertiesperson.name
Array of objectsA list of similar recordsstudents[0].name
Object of arraysNamed groups of related listscourse.titles[1]
Study tip: When you see an expression such as students[0].name, read it from left to right: first select the array element, then select the object's property.