How to easily truncate an array with JavaScript?

后端 未结 6 1246
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 16:09

Linq has a convenient operator method called Take() to return a given number of elements in anything that implements IEnumerable. Is there anything

6条回答
  •  青春惊慌失措
    2020-12-02 16:55

    There is a slice method

    array.slice(0, 4);
    

    Will return the first four elements.

    Don't forget to assign it back to your variable if you want to discard the other values.

    Note: This is just regular javascript, no need for jquery.

提交回复
热议问题