Mastering Data Aggregation and Data Streams with JSON in JavaScript

Introduction

Welcome to our lesson on mastering data aggregation and data streams with JSON formatting in JavaScript. In this lesson, we'll start by building a basic sales records aggregator. Then, we'll extend its functionality to handle more complex operations such as filtering, data aggregation, and formatting. By the end of this session, you'll be able to manage and format data streams efficiently.

Starter Task Methods and Their Definitions

To begin, we'll implement a basic sales record aggregator. Here are the methods we'll be focusing on:

  • add_sale(sale_id, amount) - Adds a sale record with a unique identifier sale_id and an amount. If a sale with the same sale_id already exists, it updates the amount.
  • get_sale(sale_id) - Retrieves the sale amount associated with the sale_id. If the sale does not exist, it returns undefined.
  • delete_sale(sale_id) - Deletes the sale record with the given sale_id. Returns true if the sale was deleted and false if the sale does not exist.

Are these methods clear so far? Great! Let's now look at how we would implement them.

Starter Task Solution

Here is the complete code for the starter task:

class SalesAggregator {
    constructor() {
        this.sales = {};  // Initialize an empty object to store sales records.
    }

    add_sale(sale_id, amount) {
        this.sales[sale_id] = amount;  // Add or update the sale with the provided sale_id and amount.
    }

    get_sale(sale_id) {
        // Retrieve and return the sale amount for the given sale_id, or undefined if it doesn't exist.
        return this.sales[sale_id] !== undefined ? this.sales[sale_id] : undefined;
    }

    delete_sale(sale_id) {
        if (sale_id in this.sales) {
            delete this.sales[sale_id];  // Remove the sale record.
            return true;  // Return true indicating the sale was deleted.
        }
        return false;  // Return false if the sale_id doesn't exist.
    }
}

// Example Usage
const aggregator = new SalesAggregator();

// Add sales
aggregator.add_sale('001', 100.50);
aggregator.add_sale('002', 200.75);

// Get sale
console.log(aggregator.get_sale('001'));  // Output: 100.5

// Delete sale
console.log(aggregator.delete_sale('002'));  // Output: true
console.log(aggregator.get_sale('002'));  // Output: undefined

Explanation:

  • The constructor method initializes an empty object to store sales records.
  • The add_sale method adds a new sale or updates the amount for an existing sale ID.
  • The get_sale method retrieves the amount for a given sale ID or returns undefined if the sale does not exist.
  • The delete_sale method removes the sale record for the given sale ID or returns false if the sale does not exist.

Now that we have our basic aggregator, let's extend it to include more advanced functionalities.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal