用Golang实现JS常用数组方法

7/9/2023 Golang

Golang切片不像JS中的数组,一些常用的方法得自己实现

# 1. push

func push(s []int, e int) []int {
  return append(s, e)
}

# 2. pop

func pop(s []int) ([]int, int) {
  l := len(s)
  if l == 0 {
    return l, 0
  }
  return s[:l-1], s[l-1]
}

# 3. unshift

func unshift(s []int, e int) []int {
  return append([]int{e}, s...)
}

# 4. shift

func shift(s []int) ([]int, int) {
  return s[1:], s[0]
}

# 5. slice

func sliceInGo(s []int, start int, end int) []int {
  return s[start: end]
}

# 6. splice

func splice(s []int, start int, deleteCount int, elements ...int) []int {
  end := start + deleteCount
  s = append(s[:start], elements... ,s[end:]...)
  return s
}

# 7. concat

func concat(s1, s2 []int) []int {
  return append(s1, s2...)
}

# 8. indexOf

func indexOf(s []int, e int) int {
  for i, v := range s {
    if v == e {
      return i
    }
  }
  return -1
}

# 9. includes

func includes(s []int, e int) bool {
   for _, v := range s {
    if v == e {
      return true
    }
  }
  return false
}

# 10. reverse

func reverse(s []int) []int {
  length := len(s)
  for i := 0; i < length/2; i++ {
    s[i], s[length-1-i] = s[length-1-i], s[i]
  }
  return s
}

# 11. map

func mapInGo(s []int, cb func(v int, idx int) int) []int {
  ret := make([]int, len(s))
  for i, v := range s {
    ret[i] = cb(v, idx)
  }
  return ret
}

# 12. forEach

func forEach(s []int, cb func(v int, idx int)) {
  for i, v := range s {
    cb(v, i)
  }
}

# 13. filter

func filter(s []int, cb func(v int, idx int) bool) []int {
  ret := make([]int, 0)
  for i, v := range s {
    if cb(v) {
      ret = append(ret, v)
    }
  }
  return ret
}
    我想,
    在这个世界上,
    虽然没有最美好的相遇,
    但却应该有为了相遇或重逢,
    所做的最美好的努力。
    红莲华
    x
    loading...