Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript dynamic arrays

var input = []; // initialise an empty array
var temp = '';
do {
    temp = prompt("Enter a number. Press cancel or leave empty to finish.");
    if (temp === "" || temp === null) {
        break;
    } else {
        input.push(temp);  // the array will dynamically grow
    }
} while (1);
Comment

dynamic array in javascript

class DynamicArray{
    constructor(){
        this.length=0;
        this.data={}
    }

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

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

        return this.length;
    }

    pop(){
        if(this.length == 0)
            return undefined;
        
        const popElement = this.data[this.length-1];
        delete this.data[this.length-1];
        this.length--;

        return popElement;
    }

    insert(element, index){
        if(index> this.length-1 || index<0)
            return undefined;
        
        this.length++;
        for(let i= this.length-1; i>index; i--){
            this.data[i] = this.data[i-1];
        }
        this.data[index] = element;
        return this.data;
    }

    remove(index){
        if(this.length == 0)
            return undefined;
        
        if(index > this.length-1 || index < 0)
            return undefined;
        
        const removedItem = this.data[index];
        for(let i=index; i < this.length; i++){
            this.data[i]=this.data[i+1];
        }
        delete this.data[this.length-1];
        this.length--;

        return removedItem;
    }
}



const array = new DynamicArray();
array.push('Aayush');
array.push('Parth');
array.push('Abhishek');
array.push('Thalesh');
array.push('chiku');

console.log(array);
array.insert('Zoya',2);
console.log(array);
Comment

PREVIOUS NEXT
Code Example
Javascript :: js string count 
Javascript :: javascript select change selected 
Javascript :: how to round numbers in javscript 
Javascript :: js double exclamation mark 
Javascript :: js array find regex 
Javascript :: vue 3 cdn example 
Javascript :: react focus textarea 
Javascript :: javascript json deserialize 
Javascript :: upload preview image jquery 
Javascript :: react google map 
Javascript :: javaScript setSeconds() Method 
Javascript :: find even numbers in an array javascript 
Javascript :: what is the use of bind method in javascript 
Javascript :: js object loop 
Javascript :: javascript extract date from string 
Javascript :: update object in array if id exists else append javascript 
Javascript :: npm install nodemon 
Javascript :: sort strings javascript alphabetically 
Javascript :: run onclick function once 
Javascript :: async awiat 
Javascript :: nodejs for windows 7 
Javascript :: how to get output of console.log in a file in javascript 
Javascript :: getdisplaymedia screenshot 
Javascript :: js get selected option elemeng 
Javascript :: oneerror javascript image 
Javascript :: how to insert an item into an array at a specific index javascript 
Javascript :: how to add checked in javascript 
Javascript :: gesture handling with react native expo 
Javascript :: descending order in objects in js 
Javascript :: js check if undefined 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =