Open In App

Implementation of Array class in JavaScript

Last Updated : 17 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This article implements Arrays using JavaScript. An Array is a simple data Structure, in which elements are stored in contiguous memory locations. Implementation of arrays performs various operations like push (adding element), pop (deleting element) element at the end of the array, getting the element from a particular index, and inserting and deleting an element from a particular index.

Implementing Array class in JavaScript: 

javascript




// User defined class Array
class Array {
 
    // Create constructor
    constructor() {
     
        // It store the length of array.
        this.length = 0;
         
        // Object to store elements.
        this.data = {};
    }
}


Output


In the above example, create a class Array which contains two properties i.e. length and data, where length will store the length of an array and data is an object which is used to store elements. 

Custom method implementations of array class:

Push(element) Method:

This method is used to push an element at the end of the array. 

javascript




push(element) {
    this.data[this.length] = element;
    this.length++;
    return this.data;
}


Pop() Method:

It is used to delete an element at the end of the array. 

javascript




pop() {
    let item = this.data[this.length-1];
    delete this.data[this.length-1];
    this.length--;
    return this.data;
}


In the above example, the item variable will store the last element from the data object and perform deletion of the last element and then, it will decrease the length by 1 and return the object. 

insertAt() Method:

This function is used to insert an element at the given index. 

javascript




insertAt(item, index) {
    for (let i = this.length; i >= index; i--) {
        this.data[i] = this.data[i - 1];
    }
    this.data[index] = item;
    this.length++;
    return this.data;
}


This function accepts two parameters item and index. The index number denotes the place where data is to be inserted and the item is the value that is to be inserted at the index. 

deleteAt(index) Method:

This function is used to remove an element at a given index or property in a data object. 

javascript




deleteAt(index) {
    for (let i = index; i < this.length - 1; i++) {
        this.data[i] = this.data[i + 1];
    }
    delete this.data[this.length - 1];
    this.length--;
    return this.data;
}


In the above function, use a loop to reach at index till the end, and copy the next element at the index, and at the end of the loop, two copies of the last element exist, delete the last element through the delete operator. 

getElementAtIndex(index) Method:

It returns the element at a given index. 

javascript




getElementAtIndex(index) {
    return this.data[index];
}


Implementation of The Array Class along with Custom Methods:

Example: This function describes the implementation of the array class and its various operations. 

javascript




class Array {
 
    // Array constructor
    constructor() {
        this.length = 0;
        this.data = {};
    }
     
    // Custom methods
    // Return data at given index
    getElementAtIndex(index) {
        return this.data[index];
    }
     
    // Push given element in the end
    push(element) {
        this.data[this.length] = element;
        this.length++;
        return this.length;
    }
     
    // Remove last element
    pop() {
        const item = this.data[this.length - 1];
        delete this.data[this.length - 1];
        this.length--;
        return this.data;
    }
     
    // Delete element at given index
    deleteAt(index) {
        for (let i = index; i < this.length - 1; i++) {
            this.data[i] = this.data[i + 1];
        }
        delete this.data[this.length - 1];
        this.length--;
        return this.data;
    }
     
    // Insert element at certain index
    insertAt(item, index) {
        for (let i = this.length; i >= index; i--) {
            this.data[i] = this.data[i - 1];
        }
        this.data[index] = item;
        this.length++;
        return this.data;
    }
}
 
// We are instantiating an object of Array class
const array = new Array();
 
// Pushing element
array.push(12);
array.push(13);
array.push(14);
array.push(10);
array.push(989);
console.log("Print element in an array");
 
for (let key in array.data) {
    console.log(array.data[key]);
}
 
// Remove last element
console.log("Pop element in an array");
array.pop(); // Popping element 989
for (var key in array.data) {
    console.log(array.data[key]);
}
 
// Insert element at certain index
console.log("Inserting element at position 2");
array.insertAt(456, 2); // Inserting element 456
for (let key in array.data) {
    console.log(array.data[key]);
}
 
// Delete element at certain index
console.log("deleting element at position 3");
array.deleteAt(3); // Deleting 14
for (let key in array.data) {
    console.log(array.data[key]);
}
 
// Get element present at certain inedx
console.log("Getting element at position 2");
console.log(array.getElementAtIndex(2));


Output:

Print element in an array
12
13
14
10
989
Pop element in an array
12
13
14
10
Inserting element at position 2
12
13
456
14
10
deleting element at position 3
12
13
456
10
Getting element at position 2
456


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads