Implementation of Array class in JavaScript
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
class Array {
constructor() {
this .length = 0;
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.
Custom method implementations of array class:
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;
}
|
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 {
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();
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]);
}
console.log( "Pop element in an array" );
array.pop();
for ( var key in array.data) {
console.log(array.data[key]);
}
console.log( "Inserting element at position 2" );
array.insertAt(456, 2);
for (let key in array.data) {
console.log(array.data[key]);
}
console.log( "deleting element at position 3" );
array.deleteAt(3);
for (let key in array.data) {
console.log(array.data[key]);
}
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
Last Updated :
17 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...