Is it possible, in Javascript, to create an array whose length is guaranteed to remain the same?
For example, the array A
is created with length 2. Subs
You can implement a class with a capacity. Lets say you want the length to remain at 5 when while pushing into the array. If you run the code snippet, you will see that 6 did not push into the array because the capacity is already met. Best Regards.
class capArray{
constructor(capacity){
this.capacity = capacity;
this.arr = [];
}
}
capArray.prototype.push = function(val){
if(this.arr.length < this.capacity) {
this.arr.push(val);
}
}
var newArray = new capArray(5);
newArray.push(1)
newArray.push(2)
newArray.push(3)
newArray.push(4)
newArray.push(5)
newArray.push(6)
console.log(newArray)
console.log(newArray.arr)
You can simply use like this.
let myArray = [];
function setItem (array, item, length) {
array.unshift(item) > length ? array.pop() : null
}
// Use Like this
setItem(myArray, 'item', 5);
Basically it will fill items in array until length goes to 5 if length will grater the 5. It pop-out las item array. So it will maintain the length always 5.
The current answer is YES, you can. There are severals ways to do that, but some web browsers has it's own "interpretation".
var x = new Array(10).fill(0);
// Output: undefined
Object.freeze(x);
// Output: Array [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
x.push(11)
// Output: TypeError: can't define array index property past the end of an array with non-writable length
x.pop()
// Output: TypeError: property 9 is non-configurable and can't be deleted [Learn More]
x[0]=10
// Output: 10 // You don't throw an error but you don't modify the array
x
// Output: Array [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
Is important to notice that if the array are object, you need to do, deep freeze instead. The code of deepfreeze is here.
A Class that wraps an Array (it's better if you don't want to throw an exception)
With ES2015 code should work the follow solution but it doesn't:
var x = new Array(10).fill(0);
Object.freeze( x.length );
x.push(3);
console.log(x);
I've written a array-fixed https://github.com/MatrixAI/js-array-fixed which is a library providing you with fixed length arrays and fixed length dense arrays (arrays which always has its elements collapsed left or collapsed right).
It supports many standard array operations such as splice and slice. But more operations can be added in the future.
The concept of push
doesn't make sense, instead there is caret*
methods available that insert an element and push out elements that already exist into empty slots.
We can use closure for this type of problem. We are just fixed the array size and return a function from a function.
function setArraySize(size){
return function(arr, val) {
if(arr.length == size) {
return arr;
}
arr.push(val);
return arr;
}
}
let arr = [];
let sizeArr = setArraySize(5); // fixed value for fixed array size.
sizeArr(arr, 1);
sizeArr(arr, 2);
sizeArr(arr, 3);
sizeArr(arr, 4);
sizeArr(arr, 5);
sizeArr(arr, 6);
console.log('arr value', arr);
I know this is an old question but nowadays there's a node module that does just this called fixed-array