Implement Pair class in JavaScript
This post will discuss how to implement a pair class in JavaScript.
A pair class is a data structure that can store two values of any type, and can be used to return multiple values from a function, or to represent key-value pairs, tuples, coordinates, etc. JavaScript does not have a native support for pairs, but there are several alternatives to implement them using existing data structures. Here are some of the ways to implement a pair class in JavaScript:
1. Using an array
We can use an array of length two to store a pair of values. We can access the first and second values by using the index 0 and 1, respectively. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 |
// create a pair of string and number var pair = ["one", 1]; // Access the first and the second value console.log(pair[0]); // "one" console.log(pair[1]); // 1 |
2. Using an object
An object is another built-in data structure in JavaScript that can store key-value pairs of any type. An object can be used to implement a pair by using the { } syntax to create an object with two properties. We can name the properties as we like, such as first and second, key and value, x and y, etc. We can access the values by using the dot notation or the bracket notation. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 |
// Create an object with two properties const pair = {first: "one", second: 1}; // Access the first value using dot notation console.log(pair.first); // one // Access the second value using bracket notation console.log(pair["second"]); // 1 |
3. Using a custom class
We can create a custom class to represent a pair of values. We can define a constructor that takes two arguments and assigns them to the instance properties. We can also define functions or getters to access or manipulate the values. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Create a custom class class Pair { // Define a constructor that takes two arguments constructor(first, second) { // Assign the arguments to the instance properties this.first = first; this.second = second; } // Define a function to get the sum of the values getSum() { return this.first + this.second; } } // Create an instance of the class const pair = new Pair(10, 20); // Access the first and the second value using dot notation console.log(pair.first); // 10 console.log(pair.second); // 20 // Call the function to get the sum of the values console.log(pair.getSum()); // 30 |
4. Using a Map
A Map is a built-in data structure in JavaScript that can store key-value pairs of any type. A Map can be used to implement a pair by using the new Map() constructor to create an empty map, and using the set() function to add two key-value pairs to the map. We can then access the values by using the get() function. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// create an empty Map var pair = new Map(); // add a key-value pair of string and string pair.set("first", "one"); // add a key-value pair of string and number pair.set("second", 1); // Access the first and the second value using get function console.log(pair.get("first")); // "one" console.log(pair.get("second")); // 1 |
That’s all about implementing a pair class in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)