在javascript中获取数组子索引的便捷方法

时间:2012-08-02 16:57:03

标签: javascript

有没有方便的方法来说明这个数组的前n个索引:

var alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];

并将其存储在另一个数组中,比如说alphabet2 = ['a','b','c'];而不必用循环强力?感谢

1 个答案:

答案 0 :(得分:4)

使用the slice method

> var alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
> alphabet.slice(0,3)
["a", "b", "c"]