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 :: run jest test for a single file 
Javascript :: loop through nested json object typescript 
Javascript :: vue displaying a this.length 
Javascript :: Sequelize.Op; 
Javascript :: convert exp date token to date 
Javascript :: @click vue target 
Javascript :: express.js get params 
Javascript :: react router redirect with query params 
Javascript :: truthy and falsy values in javascript 
Javascript :: js how to calculate factorial 
Javascript :: javascript divide string into two parts 
Javascript :: javascript deconstruct object 
Javascript :: String variable props 
Javascript :: buffer to image nodejs 
Javascript :: perform a function on each element of array javascript 
Javascript :: javascript array flatten 
Javascript :: javascript convert utc to local time 
Javascript :: rect to rect collision 
Javascript :: jquery selectors attribute ends with 
Javascript :: sweetalert example 
Javascript :: redux state proxy 
Javascript :: Stop setInterval call in JavaScript 
Javascript :: google geocode nodejs 
Javascript :: trailing comma javascript 
Javascript :: javascript multidimensional array 
Javascript :: math.sign 
Javascript :: vuejs v-model select 
Javascript :: How to blacklist words with discord.js 
Javascript :: change object in array in state 
Javascript :: create mongodb express server npm 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =