如何使用JavaScript轻松截断数组? [英] How to easily truncate an array with JavaScript?

查看:91
本文介绍了如何使用JavaScript轻松截断数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linq有一个方便的运算符方法,名为 Take(),可以在任何实现 IEnumerable 。 jQuery中有没有类似于使用数组的东西?

Linq has a convenient operator method called Take() to return a given number of elements in anything that implements IEnumerable. Is there anything similar in jQuery for working with arrays?

推荐答案

有一个切片方法

array.slice(0, 4);

将返回前四个元素。

如果要放弃其他值,请不要忘记将其分配回变量。

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

注意:这只是普通的javascript,不需要jquery。

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

这篇关于如何使用JavaScript轻松截断数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆