如何作为数组访问JavaScript对象



在gopherjs中,对象的成员本身就是对象(就像js在引擎盖下处理事物一样,ttbomk(。

因此,当访问成员时,一个人恢复了 *js.Object

func makeRID(o *js.Object) string {
  // access the array of numbers in member RID:
  arr := o.Get("RID")
  // arr is now a *js.Object - not an []interface{} - which is what I actually need (and what the contents of the property RID really are...
}

传入的o是从服务器的JSON响应中提取的JS对象。它的成员" rid"是字节值的数组 - 例如{ RID: [ 136, 210, 92, 97, 152, 26, 17, 233, 147, 147, 8, 0, 39, 223, 163, 7 ], ...

我没有谷歌搜索,也没有在JS文档中看到Gopherjs的任何明确指示,即如何从 *js.Object到Gopherjs等于JS数组,即[]interface{}

想法?

*js.Object -> []interface{}

woo -hoo-得到它!感谢您的建议:(

func makeRID(o *js.Object) string {
  // access the array of numbers in member RID:
  jsRID := o.Get("RID")
  // I know that the source object's RID member is an Array, so we can access it in gopherjs as:
  jsArray := jsRID.Interface().([]interface{})
  // ... profit!
}

最新更新