gpt4 book ai didi

javascript - 从对象数组中的字符串中查找数值并进行计算

转载 作者:行者123 更新时间:2023-11-30 13:59:42 24 4
gpt4 key购买 nike

我的 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);

关于javascript - 从对象数组中的字符串中查找数值并进行计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com