6

I know this is a basic question, I have limited knowledge in JS. Is there an alternative method to keep an array length fixed with queue feature. The array keep receiving data I want always to remove the first element of the array?

var a=Math.random;
var array=[];

if (array.length < 4) {
array.push(a);
} else {
array.shift();
}
console.log("array",array);

I was hoping if there is such thing as following:

var a=Math.random;
var array=[];
array.length=4
array.push(a);
console.log(array);
1
  • No, no native feature like that. You'd have to make something yourself or use a library with that feature. Commented Nov 7, 2020 at 1:33

2 Answers 2

1

You've basically got it in your first code block there, it just requires a slight adjustment of the logic. If it's a queue then you should always push the new number. You should then test if the length is greater than 4, and if it is, then use .shift() as you have done here to remove the first element.

Sign up to request clarification or add additional context in comments.

4 Comments

I was thinking to shift the array without using if-statement?
i see, so if you want to truncate an array to its last four elements (or leave it unchanged if its length is 4 or less) you could use array = array.slice(-4). No if statement.
I read about array.slice. So should I just pit array.slice at the which will keep my array length to 4
well I would put it in the same place you put array.length=4 (not valid code since the length property is read-only) in your second code block ... except I guess the push statement should go before it, not after.
0

Typically, when you implement a queue with a fixed-size array, you would not use the native push method, but would maintain indices of where the front/back of the queue is located. This way you avoid that all elements need to move each time you add an element.

class Queue {
    #front = 0;
    #rear = 0;
    #empty = true;
    #array;
    constructor(size) {
        this.#array = Array(size);
    }
    isEmpty() {
        return this.#empty;
    }
    isFull() {
        return !this.#empty && this.#front == this.#rear;
    }
    pushBack(value) {
        if (this.isFull()) return false;
        this.#array[this.#rear++] = value;
        this.#rear %= this.#array.length;
        this.#empty = false;
        return true;
    }
    popFront() {
        if (this.isEmpty()) return;
        const value = this.#array[this.#front++];
        this.#front %= this.#array.length;
        this.#empty = this.#front == this.#rear;
        return value;
    }
}

// example:

const queue = new Queue(4);
// Perform some actions
queue.pushBack(3);
queue.pushBack(8);
queue.popFront();
queue.pushBack(5);
queue.pushBack(1);
queue.pushBack(6);
console.log(queue.isFull()); // true
const success = queue.pushBack(99); // This value will not be added (queue is full)
console.log(success); // false
queue.popFront();
// Deplete the queue, printing its values:
while (!queue.isEmpty()) {
    console.log(queue.popFront()); // 5, 1, 6
}

In JavaScript the array implementation might not turn out the most efficient one (depending on several factors like what the data type is of the queue's elements). See for alternative implementations that also allow pop/push from the opposite sides: How to implement deque data structure in javascript?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.