Maximum array size

Let's say I have an array with data elements, in this example the numbers are, for example:

var a = [432, 238, 122, 883, 983]; 

And I want to limit the array, so that every time I add an element to the array, it always keeps let say say 7 or less and removes the oldest elements.

My current aproach is as follows:

 function add(x) { a.unshift(x); a = a.slice(0, 7); } 

This works fine, but is there a more elegant way to do this, as a single line or something else?

Edit: By "more elegant," I mean that I don’t need the add function, and I can just embed the code I need without printing, for example, three times, and only one line will also make the code more understandable "

+8
javascript arrays
source share
6 answers

Just add another alternative:

 a = [x, ...a.slice(0, 6)]; 

Even if I personally chose Nina Scholz solution (with the "best" condition for changing the length and for older environments where ES6 is not supported).

Literature:

+9
source share

Just adjust the length property after clicking.

 function add(x) { a.unshift(x); a.length = a.length < 7 ? a.length : 7; } 

Removes a way to check before

 function add(x) { a.unshift(x); if (a.length > 7) { a.length = 7; } } 
+5
source share

You can check the length of the array when adding and shift it (delete the first element) if the length has passed:

 function add(x) { a.push(x); if (a.length > 7) a.shift(); } 
+3
source share
 a=add(188); function add(x){ a.push(x); return a.slice(1); } 
0
source share

You can also use Array.pop . Also, if you want to add a property to an array, you can make it shared.

 Array.prototype.Limit_push = function(x) { this.unshift(x); if (this.maxLength !== undefined && this.length > this.maxLength) this.pop(); } var a = []; a.maxLength = 7 for (var i = 0; i < 10; i++) { a.Limit_push(i); console.log(a) } var b = []; for (var i = 0; i < 10; i++) { b.Limit_push(i); console.log(b) } 
0
source share

If you want, you can change the prototype of the Array object. Thus, all your arrays can have a maximum length.

It is cheap and efficient, but it may not work well with other libraries and plugins.

 Array.prototype.maxLength = Number.MAX_VALUE; Array.prototype.add = function(item) { this.push(item); this.adjustLength(); } Array.prototype.adjustLength = function() { this.length = Math.min(this.length, this.maxLength); } var a = [432, 238, 122, 883, 983]; a.maxLength = 7; a.add(1); a.add(2); a.add(3); // Ignored a.add(4); // Ignored document.body.innerHTML = '<ol start="0">'+a.map(function(i){return'<li>'+i+'</li>'}).join('')+'</ol>'; 
 ol li:before { content: '\0020\21d2\0020'; } 

If you create your own class object and delegate it to the base array, you can make it more portable and extensible.

 function MyList(arr, maxLength) { this.arr = arr || []; this.maxLength = maxLength || Number.MAX_VALUE; } MyList.prototype = { add : function(item) { this.arr.push(item); this.adjustLength(); }, adjustLength : function() { this.arr.length = Math.min(this.arr.length, this.maxLength); }, get : function() { return this.arr; } }; var a = new MyList([432, 238, 122, 883, 983], 7); a.add(1); a.add(2); a.add(3); // Ignored a.add(4); // Ignored document.body.innerHTML = '<ol start="0">'+a.get().map(function(i){return'<li>'+i+'</li>'}).join('')+'</ol>'; 
 ol li:before { content: '\0020\21d2\0020'; } 
0
source share

All Articles