-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean.dart
42 lines (41 loc) · 1.06 KB
/
clean.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
///清空数组和json空值数据
/// @param {dynamic} data 需要处理的数据
/// @param {?Boolean} removeEmptyStr 是否需移除留空字符串,默认false
/// @return {dynamec} 返回处理结果
dynamic clean (dynamic data, [bool removeEmptyStr = false]) {
dynamic res;
if(data is Map) {
res = {};
dynamic mpItem;
data.forEach((key, item) {
if(item is Map || item is List) {
mpItem = clean(item, removeEmptyStr);
} else {
mpItem = item;
}
_filterData(mpItem, removeEmptyStr, (useValue) {
res[key] = useValue;
});
});
} else if(data is List) {
res = [];
for(var item in data) {
_filterData(clean(item, removeEmptyStr), removeEmptyStr, (useValue) {
res.add(useValue);
});
}
} else {
res = data;
}
return res;
}
// 处理这是数据是否需要
void _filterData (dynamic data, bool removeEmptyStr, Function callback) {
if(removeEmptyStr) {
if(data!='' && data !=null) {
callback(data);
}
} else if(data !=null){
callback(data);
}
}