Approach 2 : Aggregation using Arrays

  • Create an object that contains an array property to hold aggregated items.
  • Perform operations like adding, removing, or manipulating items within the array.
  • Aggregation starts with collecting data from various sources, such as user input, databases, or external files.
  • Typically, store the collected data in an array data structure capable of holding multiple values of the same type.
  • Arrays provide a way to represent a collection of related data items.
  • Each item in the array is stored at a specific index, allows easy access and manipulation of individual elements.
  • Arrays are often used to group and classify data, allowing effective organization, such as storing scores of students in a class.

Example: In this example, player objects are created using a constructor function, and a team object is formed to aggregate these players. The team object includes methods to add, remove, and display players. Additional players are added, the team is displayed, a player is removed, and the updated team is displayed.

Javascript




function Players(name, age) {
    this.name = name;
    this.age = age;
}
 
// Create Player objects
const player1 = new Players("Kumar", 31);
const player2 = new Players("Bob", 29);
const team = {
    name: "Red Team",
    players: [player1, player2],
    addPlayer: function (player) {
        this.players.push(player);
    },
    removePlayer: function (playerName) {
        const indexToRemove =
            this.players.findIndex(player => player.name === playerName);
        if (indexToRemove !== -1) {
            this.players.splice(indexToRemove, 1);
        }
    },
    displayTeam: function () {
        console.log(`Team: ${this.name}`);
        console.log("Players:");
 
        this.players.forEach((player, index) => {
            console.log(
            `${index + 1}. Name: ${player.name}, Age: ${player.age}`);
        });
    }
};
 
// Add more players to the team
const player3 = new Players("Raj", 32);
team.addPlayer(player3);
 
// Display the team and its players
team.displayTeam();
team.removePlayer("Bob");
 
// Display the updated team
team.displayTeam();


Output

Team: Red Team
Players:
1. Name: Kumar, Age: 31
2. Name: Bob, Age: 29
3. Name: Raj, Age: 32
Team: Red Team
Players:
1. Name: Kumar, Age: 31
2. Name: Raj, Age: 32


Aggregation in JavaScript

Aggregation is a fundamental concept in object-oriented programming that facilitates the creation of complex objects by combining simpler ones. It establishes a “has-a” relationship between objects, where one object contains references to other objects as its components. This relationship enhances code reusability and modularity in JavaScript applications.

Table of Content

  • Aggregation using Object Properties
  • Aggregation using Arrays

Similar Reads

Approach 1: Aggregation using Object Properties

Create an object that contains properties representing the components you want to aggregate. These properties can be references to other objects, functions, or any data type. Establish an aggregation relationship by including one object within the other as a property. Allow one object to reference and use another, providing a way to model aggregation....

Approach 2 : Aggregation using Arrays

...

Contact Us