简体   繁体   中英

How to access a JavaScript object as an array

In GopherJS, members of an object are themselves objects (just as js handles things under the hood, ttbomk).

So when accessing a member, one gets back a *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...
}

the incoming o is a js object that was extracted from a JSON response from the server. Its member "RID" is the array of byte values - eg { RID: [ 136, 210, 92, 97, 152, 26, 17, 233, 147, 147, 8, 0, 39, 223, 163, 7 ], ...

I'm not googling nor seeing in the js Docs for gopherjs any clear indication of how to go from a *js.Object to a gopherjs equivalent of an js array, namely []interface{}

Ideas?

*js.Object -> []interface{}

Woo-hoo - got it! Thanks for suggestions :)

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!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM