javascript - 从对象数组中查找字符串中的数值并进行计算
问题描述
我的 JavaScript 数组看起来像这样。
[
{id:1, msg:"Your Acct 123XXXX456 Has Been Debited with USD2,100.00 On 05-
MAY- 2019 07:26:58 By AB: 123456**7899/USER NAME/0505201. Bal:
USD973.28CR"}, <br/>
{id:1, msg: "Your Acct 123XXXX456 Has Been Debited with USD1,100.00 On
05-MAY-2019 07:26:58 By AB: 123456**7899/USER NAME/0505201. Bal:
USD673.28CR"},<br/>
{id:2, msg: "Your Acct 345XXXX678 Has Been Debited with USD4,100.00 On
05-MAY-2019 07:26:58 By AB: 11111**22222/USER NAME/0505201. Bal:
USD373.28CR"}
]
我需要选择以下细节:
(1) Highest Debit amount (for particular user)
(2) Lowest Debit amount (for particular user)
(3) Sum all the debit amount for particular user.
我尝试了以下方法从字符串中获取非数字。
let result = myArr[0].replace(/\D+/g, ' ').trim().split(' ').map(e =>
parseInt(e));
但是输出是这样的。
[123,456,2,1,00,5]
这种方法是在每个数字前面加上逗号,用前面的 00 删除小数点。我不知道如何只选择借方金额。我期待这样的输出:
User Highest Debit Lowest Debit Total Debit
1 2,100.00 1,100.00 3,200.00
2 4,100.00 4,100.00 4,100.00
myArr = [{
"id": 1,
"msg": "Your Acct 123XXXX456 Has Been Debited with USD2,100.00 On 05- MAY - 2019 07: 26: 58 By AB: 123456 ** 7899 / USER NAME / 0505201. Bal: USD973 .28 CR "
},
{
"id": 1,
"msg": "Your Acct 123XXXX456 Has Been Debited with USD1,100.00 On 05 - MAY - 2019 07: 26: 58 By AB: 123456 ** 7899 / USER NAME / 0505201. Bal: USD673 .28 CR "
},
{
"id": 2,
"msg": "Your Acct 345XXXX678 Has Been Debited with USD4,100.00 On 05 - MAY - 2019 07: 26: 58 By AB: 11111 ** 22222 / USER NAME / 0505201. Bal: USD373 .28 CR "
}
]
let result = myArr[0].replace(/\D+/g, ' ').trim().split(' ').map(e =>
parseInt(e));
console.log(result)
解决方案
你的正则表达式太简单了
这是使用 Math.min 和 Math.max 获取每个借记项的一种方法
我正在考虑使用 reduce 但这更具可读性
myArr = [
{ "id": 1, "msg": "Your Acct 123XXXX456 Has Been Debited with USD2,100.45 On 05 - MAY - 2019 07: 26: 58 By AB: 123456 ** 7899 / USER NAME / 0505201. Bal: USD973 .28 CR " },
{ "id": 1, "msg": "Your Acct 123XXXX456 Has Been Debited with USD1,100.50 On 05 - MAY - 2019 07: 26: 58 By AB: 123456 ** 7899 / USER NAME / 0505201. Bal: USD673 .28 CR " },
{ "id": 2, "msg": "Your Acct 345XXXX678 Has Been Debited with USD4,100.00 On 05 - MAY - 2019 07: 26: 58 By AB: 11111 ** 22222 / USER NAME / 0505201. Bal: USD373 .28 CR "}
]
let res = {}
myArr.forEach(obj => {
const id = obj.id;
const msg = obj.msg;
const uPos = msg.indexOf("USD"); // assuming a static message
// grab the amount, use + to convert to number after removing the thousand separator
const num = +msg.substring(uPos+3,msg.indexOf(" ",uPos)).replace(/,/g,"")
let cur = res[id];
if (!cur) { res[id]={}; cur=res[id]; cur.total=0;}
cur.low = cur.low ? Math.min(cur.low, num) : num;
cur.high = cur.high ? Math.max(cur.high,num) : num;
cur.total += num;
})
console.log(res);
推荐阅读
- r - R中混合数据采样的回归
- firebase - 有没有办法在颤振应用程序中使用firebase查找具有相同子名的孩子的总数?
- php - 如何测试 Laravel 中 JSON 文件中存在的嵌套 JSON 数据?
- powershell - 在 For Each 语句中过滤 Powershell 结果
- symfony - 如何在 Shopware 6 中使用多个队列工作者?
- android - 如何为 Android 应用程序中的多种登录方式构建活动/片段
- python - 熊猫:回顾过去
- python - MongoDB对属性的最接近匹配给出属性的一部分
- javascript - 如何使用重叠 div 获取当前悬停位置?
- c# - 为什么在将值传递给另一个 C# 表单到文本框字段时 SELECT 不选择 TESTID?