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

标签 javascript reactjs

我的 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/

相关文章:

javascript - 安装react-router-dom时遇到错误

node.js - npm start 出现问题(错误 : spawn cmd ENOENT)

javascript - Jest 预期模拟函数已被调用,但未被调用

javascript - AWS Cognito GetUser API 未返回所有属性

javascript - html5 facebook 像按钮在本地不起作用

javascript - 在启动时使用 Meteor FS Collections 将 wav 转换为 mp3

javascript - 视频插入 DOM 后不会在 chrome 上自动播放

JavaScript 分配变量

javascript - 如何从钩子(Hook)中的另一个组件打开模态?

node.js - 在 Heroku 上部署 React 应用和 API 的最佳实践