Limit array size

前端 未结 6 1764
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 05:25

Let\'s say I have an array with data elements, in this example numbers, like this:

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

And I want to limit t

相关标签:
6条回答
  • 2021-01-04 05:51

    You can also use Array.pop. Also if you wish to add a property to array, you can make it generic.

    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 讨论(0)
  • 2021-01-04 05:54

    Just adjust the length property after pushing.

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

    The cleanes way is to check before

    function add(x) {
        a.unshift(x);
        if (a.length > 7) {
            a.length = 7;
        }
    }
    
    0 讨论(0)
  • 2021-01-04 05:56

    Just to add another possible alternative:

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

    Even though I would personally choose Nina Scholz's solution (with a "better" condition for changing length and for older environments where ES6 is not supported)

    References:

    • Spread operator
    0 讨论(0)
  • 2021-01-04 06:00

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

    function add(x) {
        a.push(x);
        if (a.length > 7)
            a.shift();
    }
    
    0 讨论(0)
  • 2021-01-04 06:03

    If you want, you can modify the prototype of the Array object. This way all of your arrays can have their own max length.

    This is cheap and effective, 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 to an underlying array, you can make this 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 讨论(0)
  • 2021-01-04 06:09
    a=add(188);
    
    function add(x){
      a.push(x);
      return a.slice(1);
    }
    
    0 讨论(0)
提交回复
热议问题