skip to Main Content

Could someone help on how to loop over response while using substr to get each variable. The variables would be Amount, Type, Balance, Date.

I have the json response

[
    [
        "AmountTypeBalanceDate",
        "$27.90Debit$6.3011/22/22, 3:32 PM",
        "$30.00Credit$34.2011/22/22, 5:13 PM",
        "$27.90Debit$4.209/12/22, 4:01 PM",
        "$30.00Credit$32.109/12/22, 8:49 PM",
        "$27.90Debit$2.107/20/22, 10:23 AM",
        "$30.00Credit$30.007/20/22, 3:22 PM",
        "Balance: $200.30"
    ]
]

Here is my code

if (xhr.status == 200) {
    console.log("status 200");
    var parsedWalletResult = JSON.parse(this.response);
    console.log(parsedWalletResult.values[0][2]); //Get the second value
    
    // Get the amount from the second  
    var walletAmount = parsedWalletResult.values[0][2]
    walletAmount = walletAmount.substr(0, 5);
    console.log("The wallet amount is: " +  walletAmount);// The wallet amount is: $30.0
    

    
    // loop over the parsed json response
    for (const key in parsedWalletResult){
      if(parsedWalletResult.hasOwnProperty(key)){
        console.log(`${key} : ${parsedWalletResult[key]}`)
      }
    }

2

Answers


  1. You cannot use substr() if the length of your fields differ ("Debit" vs "Credit"), at least not without hassle (i.e. trying out values until one matches).

    You could use a regex though:

    const data = [
        [
            "AmountTypeBalanceDate",
            "$27.90Debit$6.3011/22/22, 3:32 PM",
            "$30.00Credit$34.2011/22/22, 5:13 PM",
            "$27.90Debit$4.209/12/22, 4:01 PM",
            "$30.00Credit$32.109/12/22, 8:49 PM",
            "$27.90Debit$2.107/20/22, 10:23 AM",
            "$30.00Credit$30.007/20/22, 3:22 PM",
            "Balance: $200.30"
        ]
    ]
    
    const result = data.map(lines => lines.slice(1, -1).map(line => line.match(/($[0-9.]+)(w+)($d+.d{2})(.*)/).slice(1)))
    console.log(result)
    Login or Signup to reply.
  2. This is a perfect scenario for Regular Expressions. If you don’t know regex, be careful, as this will be hard to maintain.

    Here’s my solution:

    (possibly a more accurate regex than the other answer)

    // I have removed the first and last elements so we just have data
    
    const transactions = [
      "$27.90Debit$6.3011/22/22, 3:32 PM",
      "$30.00Credit$34.2011/22/22, 5:13 PM",
      "$27.90Debit$4.209/12/22, 4:01 PM",
      "$30.00Credit$32.109/12/22, 8:49 PM",
      "$27.90Debit$2.107/20/22, 10:23 AM",
      "$30.00Credit$30.007/20/22, 3:22 PM",
    ];
    
    const transactionsAsObjects = transactions.map((transaction) => {
      const pattern =
        /($d+.d+)(w+)($d+.d{2})(d{1,2}?/d{1,2}/d{2}, d{1,2}:d{2} (AM|PM))/g;
      const match = pattern.exec(transaction);
      if (!match) return null;
      return {
        balance: match[1],
        type: match[2],
        amount: match[3],
        date: match[4],
      };
    });
    
    console.log(transactionsAsObjects);
    
    /* 
      {balance: '$27.90', type: 'Debit', amount: '$6.30', date: '11/22/22, 3:32 PM'}
      {balance: '$30.00', type: 'Credit', amount: '$34.20', date: '11/22/22, 5:13 PM'}
      {balance: '$27.90', type: 'Debit', amount: '$4.20', date: '9/12/22, 4:01 PM'}
      {balance: '$30.00', type: 'Credit', amount: '$32.10', date: '9/12/22, 8:49 PM'}
      {balance: '$27.90', type: 'Debit', amount: '$2.10', date: '7/20/22, 10:23 AM'}
      {balance: '$30.00', type: 'Credit', amount: '$30.00', date: '7/20/22, 3:22 PM'}
    */
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search