1

My JavaScript array looks like this.

[ 
{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"}
]

I need to pick the following details:

(1) Highest Debit amount (for particular user)
(2) Lowest Debit amount (for particular user)
(3) Sum all the debit amount for particular user.

I tried the following approach to get the non-digits from string.

let result = myArr[0].replace(/\D+/g, ' ').trim().split(' ').map(e => 
parseInt(e));

But output was like this.

[123,456,2,1,00,5]

This approach is putting comma in front of each digit, removing decimal point with preceding 00. I don't have any idea how to pick only debit amount. I am expecting output like this:

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)

1
  • I made you a snippet. myArr[0] is an object. You need to use myArr[0].msg.replace Commented Jun 11, 2019 at 7:30

1 Answer 1

2

Your regex is too simple

Here is one way to get at each debit using Math.min and Math.max

I was considering using reduce but this is more readable

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help. Will this also support decimal point? like if debit amount is 2100.50
Yes it already does. See update. The + converts to number and drops .00 in the process but not .05 for example
Nice. Thanks a lot for your time and help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.