Get Method
Get Method
The get method retrieves an item from the array by its index. This method helps us access specific elements stored in our custom array.
How It Works
- Initialization: We start by creating an instance of the
MyArrayclass. - Adding Elements: We use the
pushmethod to add elements to our array. - Retrieving an Element: We call the
getmethod with the index of the item we want to retrieve.
tip
The get method allows us to access elements by their index, similar to native JavaScript arrays.
Implementation Logic
The get method takes an index as an argument and returns the item at that index from the data object. Since the data object stores elements with their indices as keys, we can directly access the desired element using the index.
Code Example
class MyArray {
constructor() {
this.length = 0;
this.data = {};
}
/**
* Retrieves the element at the given index.
* @param {number} idx - The index of the element to retrieve.
* @returns {*} - The element at the specified index.
*/
get(idx) {
return this.data[idx];
}
}
const myArray = new MyArray();
myArray.push("apple"); // Adding an element to the array
console.log(myArray.get(0)); // Retrieving the element at index 0
Output
"apple"