# 结果转换

# List转换

var list = [{
    sex : 0,
    name : '小明',
    age : 19
},{
    sex : 1,
    name : '小花',
    age : 18
}];

var getAge = (age)=>{
    return age > 18 ? '成人' : '未成年'
}
// 利用map函数对list进行过滤
return list.map((item)=>{
    age : getAge(item.age),
    sex : item.sex == 0 ? '男' : '女',
    name : item.name
});
/*结果
[{
    "sex": "男",
    "name": "小明",
    "age": "成人"
},{
    "sex": "女",
    "name": "小花",
    "age": "未成年"
}]
*/
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

# List过滤

var list = [{
    sex : 0,
    name : '小明'
},{
    sex : 1,
    name : '小花'
}]
// 利用map函数对list进行过滤
return list.filter((item)=>item.sex == 0);
/* 结果
[{
    "sex": 0,
    "name": "小明"
}]
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# List过滤和转换

var list = [{
    sex : 0,
    name : '小明'
},{
    sex : 1,
    name : '小花'
}]
// 利用map函数对list进行过滤,然后进行转换
return list.filter((item)=>item.sex == 0).map((item)=>{
    sex : item.sex == 0 ? '男' : '女',
    name : item.name
});
/* 结果
[{
    "sex": "男",
    "name": "小明"
}]
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# List分组

// List<Map<String,Object>>
var result = [
    { xxx : 1, yyy : 2, value : 11},
    { xxx : 1, yyy : 2, value : 22},
    { xxx : 2, yyy : 2, value : 33}
];


return result.group(item=>item.xxx + '_' + item.yyy)
/*
Map<Object,List<Object>>
{
    "1_2": [
            {"yyy": 2, "xxx": 1, "value": 11},
            {"yyy": 2, "xxx": 1, "value": 22}
    ],
    "2_2": [{"yyy": 2, "xxx": 2, "value": 33 }]
}
*/

return result.group(item=>item.xxx + '_' + item.yyy,list=>{
    count : list.size(),
    sum : list.map(v=>v.value).sum(),
    avg : list.map(v=>v.value).avg()
})
/*
Map<Object,Object>
{
    "1_2": { "avg": 16.5, "count": 2, "sum": 33 },
    "2_2": { "avg": 33,   "count": 1, "sum": 33 }
}
*/
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

# List关联

var year2019 = [
    { "pt":2019, "item_code":"code_1", "sum_price":2234 },
    { "pt":2019, "item_code":"code_2", "sum_price":234 },
    { "pt":2019, "item_code":"code_3", "sum_price":12340 },
    { "pt":2019, "item_code":"code_4", "sum_price":2344 }
];
var year2018 = [
    { "pt":2018, "item_code":"code_1", "sum_price":1234.0 },
    { "pt":2018, "item_code":"code_4", "sum_price":1234.0 }
];
return year2019.join(year2018, (left,right)=>left.item_code == right.item_code,  (left,right)=>{
   '年份' : left.pt,
   '编号' : left.item_code,
   '今年' : left.sum_price,
   '去年' : right == null ? 'unknow' : right.sum_price,
   '环比去年增长' : right == null ? '-': (((left.sum_price - right.sum_price) / right.sum_price * 100) + "%")
});
/*
[
    {"年份": 2019, "今年": 2234, "去年": 1234, "环比去年增长": "81.03728%", "编号": "code_1"},
    {"年份": 2019, "今年": 234, "去年": "unknow", "环比去年增长": "-", "编号": "code_2"},
    {"年份": 2019, "今年": 12340,"去年": "unknow","环比去年增长": "-","编号": "code_3"},
    {"年份": 2019, "今年": 2344, "去年": 1234, "环比去年增长": "89.95138%", "编号": "code_4"}
]
*/
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
上次更新: 11/8/2020, 11:50:06 PM