Skip to content

Instantly share code, notes, and snippets.

@lcy101u
Last active March 30, 2022 17:06
Show Gist options
  • Save lcy101u/69db1a81103d9515aad437ea6ef4e523 to your computer and use it in GitHub Desktop.
Save lcy101u/69db1a81103d9515aad437ea6ef4e523 to your computer and use it in GitHub Desktop.
javaScript Array.slice()
const array = ['A', 'B', 'C', 'D', 'E']
console.log(array.slice()) // ['A', 'B', 'C', 'D', 'E'] ,沒有參數,全部拷貝
console.log(array.slice(5)) // [] 如果begin 超過陣列長度會回傳空字串
console.log(array.slice(-1)) // ['E'],begin 是 -1 ,表示從最後一個元素拷貝
console.log(array.slice(-2)) // ['D', 'E'],begin 是 -2,表示從最後兩個開始拷貝
console.log(array.slice(0, 2)) // ['A', 'B'] begin 是 0 到 2 (不包含2)
console.log(array.slice(0, -1)) // ['A', 'B', 'C', 'D'] begin 0 到 -1 (表示最後一個元素但不包含)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment