且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将JavaScript对象转换为Dart Map?

更新时间:2022-04-15 00:49:07

如果你想深入处理并处理其他案例那么简单映射和保留函数(与json解决方案不同)然后使用这个简单的函数:

If you want to do it deeply and handle also other cases then simple maps AND preserve functions (unlike the json solution) then use this simple function:

_toDartSimpleObject(thing) {
 if (thing is js.JsArray) {
  List res = new List();
  js.JsArray a = thing as js.JsArray;
  a.forEach((otherthing) {
    res.add(_toDartSimpleObject(otherthing));
  });
  return res;

 } else if (thing is js.JsObject) {
  Map res = new Map();
  js.JsObject o = thing as js.JsObject;
  Iterable<String> k = js.context['Object'].callMethod('keys', [o]);
   k.forEach((String k) {
   res[k] = _toDartSimpleObject(o[k]);
  });
  return res;

 } else {
  return thing;
 }
}