Limit array size

前端 未结 6 1812
隐瞒了意图╮
隐瞒了意图╮ 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: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

提交回复
热议问题