Chapter 7

Arrays & Objects

Learning Objectives

  • Master arrays for storing lists
  • Understand objects for structured data
  • Learn array methods and manipulation
  • See how MixMind manages track collections

Arrays

Arrays store ordered lists of values:

// Creating arrays
let tracks = ["song1.mp3", "song2.mp3", "song3.mp3"];
let bpms = [128, 130, 125];
let empty = [];

// Accessing elements (0-indexed)
console.log(tracks[0]); // "song1.mp3"
console.log(tracks[2]); // "song3.mp3"

// Array length
console.log(tracks.length); // 3

// Adding elements
tracks.push("song4.mp3"); // Add to end
tracks.unshift("song0.mp3"); // Add to beginning

// Removing elements
tracks.pop(); // Remove from end
tracks.shift(); // Remove from beginning

Array Methods

// Looping through arrays
for (let i = 0; i < tracks.length; i++) {
    console.log(tracks[i]);
}

// Modern looping
for (let track of tracks) {
    console.log(track);
}

// forEach
tracks.forEach((track, index) => {
    console.log(`${index}: ${track}`);
});

// map - transform each element
const uppercase = tracks.map(t => t.toUpperCase());

// filter - keep elements that match condition
const longTracks = tracks.filter(t => t.length > 10);

// find - find first match
const found = tracks.find(t => t.includes("song2"));

// reduce - combine all elements into one value
const totalDuration = bpms.reduce((sum, bpm) => sum + bpm, 0);

Objects

// Creating objects
let track = {
    title: "Summer Nights",
    artist: "DJ Mix",
    bpm: 128,
    key: "Am",
    duration: 240,
    file: "summer.mp3"
};

// Accessing properties
console.log(track.title); // "Summer Nights"
console.log(track["bpm"]); // 128

// Modifying properties
track.bpm = 130;
track.analyzed = true;

// Adding new properties
track.energy = 0.85;

// Nested objects
let mix = {
    name: "My Mix",
    tracks: [track],
    settings: {
        crossfade: 8,
        format: "mp3"
    }
};

🎵 MixMind Connection

How MixMind Uses Arrays & Objects

// Track library as array of objects
const library = [
    {
        id: 1,
        title: "Song 1",
        bpm: 128,
        key: "Am",
        buffer: audioBuffer1
    },
    {
        id: 2,
        title: "Song 2",
        bpm: 130,
        key: "C",
        buffer: audioBuffer2
    }
];

// Finding compatible tracks
const compatible = library.filter(track => {
    return Math.abs(track.bpm - currentBPM) < 10;
});

// Mix settings object
const mixSettings = {
    tracks: [],
    transitions: [],
    totalDuration: 0,
    format: "mp3",
    quality: 320
};

Exercise 7.1: Practice

Goal: Apply what you've learned in this chapter

Try the concepts from this chapter in your browser's console or a simple HTML file. Experiment with the code examples and modify them to see what happens.

📝 Quick Reference:

Review the key concepts from this chapter and make sure you understand each one before moving to the next chapter.