JSlice is a Golang package that provides a set of methods to manipulate arrays which are built-in functions in JavaScript.
I implemented this package as hobby project. I personally do not use external package for array manipulation. I prefer to implement them with for loop etc. when necessary. But if you are looking for a package that provides array manipulation methods, this package may help you.
- Get jslice package in your project
go get github.com/anilsenay/jslice
- Import jslice
import "github.com/anilsenay/jslice"
- Use jslice methods
slice := jslice.Of(1, 2, 3, 4, 5)
filteredSlice = slice.Filter(func(i int) bool {return i > 3}) // [4,5]
filteredSlice = filteredSlice.Take(1) // [4]
Chaning methods is also possible.
intSlice := []int{1, 2, 3, 4, 5}
slice = jslice.From(intSlice).Filter(func(i int) bool {
return i > 3
}).Map(func(i int) int {
return i * 2
})
fmt.Println(slice) // [8,10]
Function | JavaScript | JSlice |
---|---|---|
at() | ✅ | ✅ |
concat() | ✅ | ✅ |
copyWithin() | ✅ | ❌ |
entries() | ✅ | ❌ |
every() | ✅ | ✅ |
fill() | ✅ | ❌ |
filter() | ✅ | ✅ |
find() | ✅ | ✅ |
findIndex() | ✅ | ✅ |
findLast() | ✅ | ✅ |
findLastIndex() | ✅ | ✅ |
flat() | ✅ | ❌ |
flatMap() | ✅ | ❌ |
forEach() | ✅ | ✅ |
group() | ✅ | ❌ |
groupToMap() | ✅ | ❌ |
includes() | ✅ | ✅ |
indexOf() | ✅ | ✅ |
join() | ✅ | ✅ |
keys() | ✅ | ✅ |
lastIndexOf() | ✅ | ✅ |
map() | ✅ | ✅ |
pop() | ✅ | ✅ |
push() | ✅ | ✅ |
reduce() | ✅ | ❌ |
reduceRight() | ✅ | ❌ |
reverse() | ✅ | ✅ |
shift() | ✅ | ✅ |
slice() | ✅ | ✅ |
some() | ✅ | ✅ |
sort() | ✅ | ✅ |
splice() | ✅ | ❌ |
toLocaleString() | ✅ | ❌ |
toString() | ✅ | ✅ |
unshift() | ✅ | ✅ |
values() | ✅ | ✅ |
I am welcome to any contribution. Missing methods can be implemented. And also, you can improve the existing methods. Please feel free to open an issue or pull request.