Implementation of Array class in JavaScript

This article implementing 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 particular index, inserting and deleting element from particular index.

Array class in JavaScript:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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 array which are listed below:

  • Push()
  • Pop()
  • insertAt()
  • deleteAt()
  • getElementAtIndex()

Push(element): This function is used to push an element at the end of the array.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Pop(): It is used to delete an element at the end of the array.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

insertAt(): This function is used to insert an element at given index.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


This function accepts two parameters item and index. Index number denoting the place where data to be inserted and item is the value which is to be inserted at index.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

getElementAtIndex(index): It returns the element at given index.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        Implementation of array
    </title>
</head>
  
<body>
    <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);
    document.write("<div>Print element in an array</div>");
    for(var key in array.data){
      document.write("<span>"+array.data[key]+" "+"</span>"); 
    }
      
      
    document.write("<div>Pop element in an array</div>");
    array.pop(); //Popping element 989
    for(var key in array.data){
      document.write("<span>"+array.data[key]+" "+"</span>"); 
    }
      
      
    document.write("<div>Inserting element at position 2</div>");
    array.insertAt(456, 2); //Inserting element 456
    for(var key in array.data){
      document.write("<span>"+array.data[key]+" "+"</span>"); 
    }
      
    document.write("<div>deleting element at position 3</div>");
    array.deleteAt(3); //Deleting 14
    for(var key in array.data){
      document.write("<span>"+array.data[key]+" "+"</span>"); 
    }
      
    document.write("<div>Getting  element at position 2</div>");
    document.write("<div>"+array.getElementAtIndex(2)+"</div>");
    </script>
</body>
</html>                    

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.