为了账号安全,请及时绑定邮箱和手机立即绑定

如何以数组形式访问 JavaScript 对象

如何以数组形式访问 JavaScript 对象

Go
Qyouu 2023-06-26 17:34:47
在 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是一个 js 对象,该对象是从服务器的 JSON 响应中提取的。它的成员“RID”是字节值数组 - 例如{ RID: [ 136, 210, 92, 97, 152, 26, 17, 233, 147, 147, 8, 0, 39, 223, 163, 7 ],......我没有谷歌搜索,也没有在 gopherjs 的 js 文档中看到任何关于如何从 *js.Object 到相当于 js 数组的 gopherjs 的明确指示,即[]interface{}有想法吗?*js.Object -> []interface{}
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

呜呜——明白了!感谢您的建议:)


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!

}


查看完整回答
反对 回复 2023-06-26
  • 1 回答
  • 0 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信