-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path多方结账.js
84 lines (79 loc) · 1.57 KB
/
多方结账.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const list = [{
id: 0,
name: '喜羊羊',
amount: 92
}, {
id: 1,
name: '慢羊羊',
amount: 124
}, {
id: 2,
name: '美羊羊',
amount: 88
}, {
id: 3,
name: '沸羊羊',
amount: 98
}, {
id: 4,
name: '暖羊羊',
amount: 65
}, {
id: 5,
name: '懒羊羊',
amount: 98
}];
//遍历可能事件
const result = () => {
let r = [];
list.forEach(i => {
list.forEach(j => {
i.id !== j.id && r.push(`${i.id}-${j.id}`)
})
})
return r;
}
//格式化数组内容
const str = result().map(i => {
let k = i.split('-'), f, l;
if (Number(k[0]) < Number(k[1])) {
f = k[0];
l = k[1];
} else {
f = k[1];
l = k[0];
}
return `${f}-${l}`
});
//数组去重
const newStrArr = [...new Set(str)];
//功能:将浮点数四舍五入,取小数点后2位
function toDecimal(x) {
var f = parseFloat(x);
if (isNaN(f)) {
return;
}
f = Math.round(x * 100) / 100;
return f;
}
//得到最终结果
const getResult = () => {
let resulArr = [];
newStrArr.forEach(i => {
const iArr = i.split('-'),
firstOne = list[Number(iArr[0])],
secondOne = list[Number(iArr[1])],
listLength = list.length,
fName = firstOne.name,
sName = secondOne.name,
f_s = toDecimal(secondOne.amount / listLength),
s_f = toDecimal(firstOne.amount / listLength),
content = f_s > s_f
? `${fName} 应转给 ${sName} ${f_s - s_f} 元`
: f_s === s_f
? null
: `${sName} 应转给 ${fName} ${f_s - s_f} 元`;
content && resulArr.push(content);
})
return resulArr;
}