javascript| slice(0,0)为什么不能返回index 0的元素?

181 阅读1分钟

参考MDN developer.mozilla.org/en-US/docs/…

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

从以上定义可以看出,虽然第二个参数end是从0开始,但要注意的是,返回的数组中不包含end!!所以当start 和end相同时,返回的是[].

想要返回index 0 需要slice(0,1)