Skip to content
Related Articles
Open in App
Not now

Related Articles

Implementation of Array class in JavaScript

Improve Article
Save Article
Like Article
  • Last Updated : 15 Dec, 2022
Improve Article
Save Article
Like Article

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.

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 = {};
    }
}

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. 

Function in Array: There are many functions in an array which are listed below:

Push(element): This function 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(): 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(): 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): 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): It returns the element at a given index. 

javascript




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

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

javascript




<script>
    class Array{
        constructor(){
    this.length=0;
    this.data={};
        }
        getElementAtIndex(index){
    return this.data[index];
        }
        push(element){
    this.data[this.length]=element;
    this.length++;
    return this.length;
        }
        pop(){
    const item= this.data[this.length-1];
    delete this.data[this.length-1];
    this.length--;
    return this.data;
        }
        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;
        }
        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;
        }
        }
        const array= new Array(); //we are instantiating an object of Array class
        array.push(12);
        array.push(13); //pushing element
        array.push(14);
        array.push(10);
        array.push(989);
        console.log("Print element in an array");
        for(var key in array.data){
        console.log(array.data[key]);
        }
         
         
        console.log("Pop element in an array");
        array.pop(); //Popping element 989
        for(var key in array.data){
        console.log(array.data[key]);
        }
         
         
        console.log("Inserting element at position 2");
        array.insertAt(456, 2); //Inserting element 456
        for(var key in array.data){
        console.log(array.data[key]);
        }
         
        console.log("deleting element at position 3");
        array.deleteAt(3); //Deleting 14
        for(var key in array.data){
        console.log(array.data[key]);
        }
         
        console.log("Getting element at position 2");
        console.log(array.getElementAtIndex(2));
</script>

Output:

 


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!