Search code examples
postman

I am trying to iterate over an Array in Postman Tests tab, This Array key has spaces in it


Below is the request

{
    "quote": {
        "AdpPrefill": "N",
        "AuditFrequency": "Annual",
        
    },
    "OOSE revisions": [
        {
            "Order": "1",
            "EntityReference": "P01WK0000014120003"
        },
        {
            "Order": "2",
            "Reference": "P01WK0000014120004"
        },
        {
            "Order": "3",
            "Reference": "P01WK0000014120005"
        }
    ]
}

I would like to iterate the "OOSE Revisions" Array and want to get the latest "Reference", In this case it should be "P01WK0000014120005".


Solution

  • Ok You kind of are moving towards the right way, but you are not right there yet.

    Let's break your problem into bite size pieces.

    here's your data

    {
      "quotePolicy": {
        "AdpPrefill": "N",
        "AuditFrequency": "Annual",
      },
      "OOSE revisions": [
        {
          "Order": "1",
          "EntityReference": "P01WK0000014120003"
        },
        {
          "Order": "2",
          "EntityReference": "P01WK0000014120004"
        },
        {
          "Order": "3",
          "EntityReference": "P01WK0000014120005"
        }
      ]
    }
    

    first we will have to access the object OOSE revisions. We could do something like this.

     const revisions = jsonData['OOSE revisions'];
     const revisionsLength = revisions.length;
    

    this now contains all the revisions that you fetched and its length

    We would also need a container entityReference for latest EntityReference and also some constant maxOrder to compare the order to.

    now we need to loop through the revisions and get the latest revision

     let maxOrder;
     let entityReference;
    
     for (let i = 0; i < revisions.length; i++) {
       const currentOrder = revisions[i].Order;
       if (maxOrder === undefined || currentOrder > maxOrder) {
         maxOrder = currentOrder;
         entityReference = revisions[i].EntityReference;
       }
     }
    

    during the loop, if maxOrder is undefined or the currentOrder is greater than maxOrder, then the maxOrder and entityReference are updated accordingly ending in the entityReference to have the correct value.

     console.log(entityReference); // "P01WK0000014120005"
    

    The way I did is OK, but very convoluted, hard to understand and not very elegant.

    You can achive the same result using modern JavaScript methods like maps, filters, etc.

    Here's how you do that:

    We start out with something like this.

     const revisions = jsonData['OOSE revisions'];
    

    Then get the latest Order from the object (assuming that the Order numbers are incremental and the largest one is the latest one. By mapping through the revisions and finding the largest number using Math.max we could do something like

    const maxOrder = Math.max(...revisions.map(revision => Number(revision.Order)));
    

    Now that we have the largest/latest number of Order, all we have to do is filter the array and get the entityReference of the order matching the largest/latest Order

    const entityReference = revisions.find(revision => Number(revision.Order) === maxOrder).EntityReference;
    
     console.log(entityReference); // "P01WK0000014120005"
    

    Hope this helps