如何删除数组中的第0个索引元素并返回JavaScript中的其余元素?
_.rest()是用于返回元件的其余部分除了第零索引的元素。它属于underscore.js(一个javascript库)。它有两个参数。一个是数组 ,另一个是索引。第二个参数用于从给定的索引数组开始搜索。
语法
_.rest( array, index );
示例
在下面的示例中,使用 _.rest()方法删除了第0个索引元素。这里没有传递第二个参数。
<html>
<body>
<script
src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
</script>
</head>
<body>
<script type="text/javascript">
document.write(_.rest([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
</script>
</body>
</html>输出结果
2, 3, 4, 5, 6, 7, 8, 9, 10
在以下示例中,还包含第二个参数。当提供第二个参数,该参数仅是索引时,将删除大量元素,并显示其余元素,如输出所示。
示例
<html>
<body>
<script
src ="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js" >
</script>
</head>
<body>
<script type="text/javascript">
document.write(_.rest([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],4));
</script>
</body>
</html>输出结果
5, 6, 7, 8, 9, 10
热门推荐
10 明天早上祝福语简短的话
11 致同事升迁祝福语简短
12 搬家寄语祝福语大全简短
13 朋友被录取祝福语简短
14 小班幼儿简短过年祝福语
15 送花顾客祝福语简短的话
16 朋友布置新房祝福语简短
17 好姐妹生日祝福语 简短
18 结婚祝福语嫂子的话简短