Find Numeric Values among string from Array of Objects and do calculation

Malik Awan

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)

mplungjan

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Sorting array of objects by string vs numeric values behaving differently

分類Dev

How to put values of objects from an array into a string

分類Dev

Do calculation for array of values - Python

分類Dev

Score calculation from array values

分類Dev

trying to find the value is numeric or integer from string

分類Dev

Sort an array of objects based on a numeric key given as String

分類Dev

Array of objects find key name and merge the values

分類Dev

Find the value inside the array of objects that include children from the array of objects

分類Dev

gspread - Getting values as string from numeric like column

分類Dev

Paste values from array into string

分類Dev

Using array.map() to find elements from an array of objects

分類Dev

How to get the combination of array values from nested arrays in an array of objects

分類Dev

T-SQL, Find numeric values

分類Dev

Access (and count) just object values from Postgres JSONB array of objects

分類Dev

Fetch unique values from array of objects based on dynamically passed property

分類Dev

How to use reduce to retrieve values from deep nested array objects

分類Dev

How do I filter out values from these JSON objects

分類Dev

get first numeric values from pyspark dataframe string column into new column

分類Dev

MySQL - Select only first 3 or 2 Characters (non - numeric) values from string using REGEXP

分類Dev

Excel - Find Strings in Column That Do Not Equal Array of Values

分類Dev

Find duplicate values in objects with Javascript

分類Dev

Find string length from select form field with multiple values with javascript

分類Dev

How do I create a react list component from an array of objects?

分類Dev

How do I ORDER BY a JSON Array with its String Values?

分類Dev

JS find() not working on array of objects

分類Dev

Find Maximum of Array of Index/Match from Concatenated String

分類Dev

Match string with values from array in express-validation

分類Dev

Distinct values from array based on a part of the string in JS

分類Dev

Find the shortest string in array

Related 関連記事

  1. 1

    Sorting array of objects by string vs numeric values behaving differently

  2. 2

    How to put values of objects from an array into a string

  3. 3

    Do calculation for array of values - Python

  4. 4

    Score calculation from array values

  5. 5

    trying to find the value is numeric or integer from string

  6. 6

    Sort an array of objects based on a numeric key given as String

  7. 7

    Array of objects find key name and merge the values

  8. 8

    Find the value inside the array of objects that include children from the array of objects

  9. 9

    gspread - Getting values as string from numeric like column

  10. 10

    Paste values from array into string

  11. 11

    Using array.map() to find elements from an array of objects

  12. 12

    How to get the combination of array values from nested arrays in an array of objects

  13. 13

    T-SQL, Find numeric values

  14. 14

    Access (and count) just object values from Postgres JSONB array of objects

  15. 15

    Fetch unique values from array of objects based on dynamically passed property

  16. 16

    How to use reduce to retrieve values from deep nested array objects

  17. 17

    How do I filter out values from these JSON objects

  18. 18

    get first numeric values from pyspark dataframe string column into new column

  19. 19

    MySQL - Select only first 3 or 2 Characters (non - numeric) values from string using REGEXP

  20. 20

    Excel - Find Strings in Column That Do Not Equal Array of Values

  21. 21

    Find duplicate values in objects with Javascript

  22. 22

    Find string length from select form field with multiple values with javascript

  23. 23

    How do I create a react list component from an array of objects?

  24. 24

    How do I ORDER BY a JSON Array with its String Values?

  25. 25

    JS find() not working on array of objects

  26. 26

    Find Maximum of Array of Index/Match from Concatenated String

  27. 27

    Match string with values from array in express-validation

  28. 28

    Distinct values from array based on a part of the string in JS

  29. 29

    Find the shortest string in array

ホットタグ

アーカイブ