In this article we will learn how to implementing array Data Structure in Javascript.
An Array is a simple data Structure, in which elements are stored in contiguous memory locations.
Create a skeleton of a array class
Skeleton of a array class
class Array {
constructor() {
// length of array.
this.length = 0;
// Object to store elements.
this.data = {};
}
// Methods to implement
// push
// pop
// insertAt()
// deleteAt()
// getElementAtIndex()
}
Now we have created a skeleton for the class, and when that class is called, we can access the methods and properties of that class, like this:
const array = new Array();
But we didn't create any properties or methods for that class, we just did a skeleton, so if we print that class we won't find anything in it.
const array = new Array();
console.log(array);
// Array { length: 0, data: {} }
Now let’s see implementation of each method:
Implementation of push
method
This function is used to push an element at the end of the array.
Implementation
class Array {
constructor() {
// length of array.
this.length = 0;
// Object to store elements.
this.data = {};
}
// Push element into array
push(element) {
this.data[this.length] = element;
console.log('Inserted', this.data[this.length]);
this.length++;
return this.data;
}
}
Now that we've done the implementation, let's do a test for the array class.
// Create an array class
const array = new Array();
// Push new element
console.log(array.push('Mohamed'));
Output
Inserted Mohamed
{ '0': 'Mohamed' }
Implementation of pop
method
pop
is used to delete an element at the end of the array.
Implementation
pop() {
let item = this.data[this.length - 1];
delete this.data[this.length - 1];
console.log('Deleted', item);
this.length--;
return this.data;
}
Now that we've done the implementation, let's do a test for the array class.
// Create an array class
const array = new Array();
// Push element
console.log(array.push('Mohamed'));
// Delete element
console.log(array.pop());
Output
Inserted Mohamed
{ '0': 'Mohamed' }
Deleted Mohamed
{}
Implementation of insertAt
method
This function is used to insert an element at the given index.
Implementation
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;
}
Now that we've done the implementation, let's do a test for the array class.
// Array
const array = new Array();
// Push elements
array.push('John');
array.push('Doe');
// Print the array
console.log(array);
// `insertAt` index 1
array.insertAt('John', 1);
// Print the array
console.log(array);
Output
Inserted John
Inserted Doe
Array { length: 2, data: { '0': 'John', '1': 'Doe' } }
Array { length: 3, data: { '0': 'John', '1': 'John', '2': 'Doe' } }
Implementation of deleteAt
method
This function is used to remove an element at a given index or property in a data object.
Implementation
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;
}
Now that we've done the implementation, let's do a test for the array class.
// Array
const array = new Array();
// Push elements
array.push('John');
array.push('Doe');
// Print the array
console.log(array);
// Insert
array.insertAt('John', 1);
// Delete
array.deleteAt(1);
// Print the array
console.log(array);
Output
Inserted John
Inserted Doe
Array { length: 2, data: { '0': 'John', '1': 'Doe' } }
Array { length: 2, data: { '0': 'John', '1': 'Doe' } }
Full code:
class Array {
constructor() {
// length of array.
this.length = 0;
// Object to store elements.
this.data = {};
}
// For push new element
push(element) {
this.data[this.length] = element;
console.log('Inserted', this.data[this.length]);
this.length++;
return this.data;
}
// Delete last element of array
pop() {
let item = this.data[this.length - 1];
delete this.data[this.length - 1];
console.log('Deleted', item);
this.length--;
return this.data;
}
// Insert at a specific 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;
}
// delete from a specific 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;
}
}
Thank you for reading
Thank you for reading my blog. 🚀