traverse function

dynamic traverse(
  1. dynamic data,
  2. List<String> keys
)

Implementation

dynamic traverse(dynamic data, List<String> keys) {
  dynamic again(dynamic data, String key, {bool deadEnd = false}) {
    List<dynamic> res = [];

    if (data is Map && data.containsKey(key)) {
      res.add(data[key]);
      if (deadEnd) return res.length == 1 ? res[0] : res;
    }

    if (data is List) {
      res.addAll(
          data.map((v) => again(v, key)).expand((x) => x is List ? x : [x]));
    } else if (data is Map) {
      res.addAll(data.values
          .map((v) => again(v, key))
          .expand((x) => x is List ? x : [x]));
    }

    return res.length == 1 ? res[0] : res;
  }

  dynamic value = data;
  String? lastKey = keys.isNotEmpty ? keys.last : null;
  for (String key in keys) {
    value = again(value, key, deadEnd: lastKey == key);
  }

  return value;
}