0

Have code which generates an https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.CustomEvent.html.

Logging event.detail() results in: {index: 1}. This is of type JsValue, but how to obtain the integer 1 value in rust?

One way could be https://rustwasm.github.io/docs/wasm-bindgen/reference/iterating-over-js-values.html Other betters ways?

Jonas Bojesen
  • 855
  • 1
  • 8
  • 22

1 Answers1

2

js_sys::Reflect seems to fit for this: https://rustwasm.github.io/wasm-bindgen/api/js_sys/Reflect/fn.get.html

let js_index = js_sys::Reflect::get(&event.detail(), &JsValue::from_str("index")).unwrap();    
let i : usize = js_index.as_f64().unwrap() as usize;
Jonas Bojesen
  • 855
  • 1
  • 8
  • 22
  • Thank you Jonas your answer helped me find a solution to [my problem](https://stackoverflow.com/questions/75203156) – jq170727 Jan 27 '23 at 01:56