📚 Class 25 - Arrays, Objects & Operations

Understanding data structures and performing operations

Example 1: String Operations

Perform multiple operations on string

let text = 'Hello World';
text.slice(0, 5);      // 'Hello'
text.toUpperCase();    // 'HELLO WORLD'
text.toLowerCase();    // 'hello world'
text.split(' ');       // ['Hello', 'World']

Example 2: Array Operations

Add, remove, and extract from array

let arr = [10, 20, 30, 40, 50];
arr.push(60);     // Add to end
arr.pop();        // Remove from end
arr.unshift(5);   // Add to start
arr.shift();      // Remove from start
arr.slice(1, 4);  // Extract part

Example 3: Object Operations

Add, update, and delete object properties

let person = { name: 'John', age: 25 };
person.email = 'john@example.com';  // Add
person.age = 26;                    // Update
delete person.city;                 // Delete

Example 4: Array of Objects

Access data from array of objects

let students = [
  { name: 'John', age: 20, grade: 'A' },
  { name: 'Sarah', age: 22, grade: 'B' }
];
console.log(students[0].name); // 'John'

Example 5: Nested Object

Access nested object properties

let person = {
  name: 'John',
  address: {
    city: 'New York',
    zip: '10001'
  }
};
console.log(person.address.city); // 'New York'

Example 6: Check Array Element

Check if element is greater than threshold

Array: [5, 12, 8, 20, 3]

let numbers = [5, 12, 8, 20, 3];
let value = numbers[1]; // 12
if (value > 10) {
  console.log('Greater than 10');
}

Example 7: Find Student by Index

Get student data by index

Students: John (0), Sarah (1), Mike (2)

let students = [
  { name: 'John', age: 20 },
  { name: 'Sarah', age: 22 }
];
console.log(students[1].name); // 'Sarah'

Example 8: Merge Two Arrays

Combine two arrays into one

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let merged = [];
// Add all items from both arrays

Example 9: Extract Words from String

Split string and access words

let text = 'Hello World JavaScript';
let words = text.split(' ');
console.log(words[0]); // 'Hello'
console.log(words[1]); // 'World'

Example 10: Calculate Item Total

Calculate total price for an item

let item = {
  name: 'Apple',
  price: 50,
  quantity: 3
};
let total = item.price * item.quantity; // 150

📝 Quick Reference

✅ Remember:

  • Arrays - Store multiple items in order (use index)
  • Objects - Store data as key-value pairs
  • Nested structures - Arrays/objects inside arrays/objects
  • Array index starts at 0
  • Use dot notation for object properties
  • String methods don't modify original