Skip to content

Instantly share code, notes, and snippets.

@AmirMahdyJebreily
Created February 11, 2025 16:17
Show Gist options
  • Save AmirMahdyJebreily/d3a1c8968588a18c0b566f79b64dd123 to your computer and use it in GitHub Desktop.
Save AmirMahdyJebreily/d3a1c8968588a18c0b566f79b64dd123 to your computer and use it in GitHub Desktop.
A function that converts a golang 2d slice into a js 2d array
//go:build js && wasm
package main
import (
"iter"
)
// func js.ValueOf(x any) js.Value
// ValueOf returns x as a JavaScript value:
// | Go | JavaScript |
// | ---------------------- | ---------------------- |
// | js.Value | [its value] |
// | js.Func | function |
// | nil | null |
// | bool | boolean |
// | integers and floats | number |
// | string | string |
// | []interface{} | new array |
// | map[string]interface{} | new object |
func Convert2dArrayToJsArray[T any](goArr [][]T) iter.Seq2[int, []interface{}] {
return func(yeild func(index int, items []interface{}) bool) {
for i := range goArr {
r := make([]interface{}, len(goArr[i]))
for j := range goArr[i] {
r[j] = goArr[i][j]
}
if !yeild(i, r) {
return
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment