-1

I have a JSON response body that looks like this:

[
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "PT1011",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2022-10-14T20:10:42.8276862+00:00"
        
    },
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "JD2994",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2023-03-31T16:23:19.5981913+00:00"
    }
]

I want to loop through the response body in Postman, find the MAX CreatedDate and store the ExperimentId associated with it, in a variable. So in the example above I'd want to store JD2994 in a variable.

I started with this, but am getting a max is not defined error:

var jsonData = JSON.parse(responseBody)

for (var i=0, len = jsonData.length; i<len; i++) {
  var value = max(jsonData[i]["CreatedDate"]);
}
JD2775
  • 3,658
  • 7
  • 30
  • 52
  • 1
    What do you expect `max` to do for you? and you overwrite it every time so you will only get the last – mplungjan Mar 31 '23 at 17:03
  • That's the idea. My goal is to retrieve the ExperimentId that was just generated in POST, and use that in the next GET request. Unfortunately the ExperimentId is not returned in the POST response body, so I need to rely on the max CreatedDate to grab it. I will always be looking for the max date in this case – JD2775 Mar 31 '23 at 17:06
  • you can do it very simple way `const maxDate = new Date( Math.max( ...data.map(element => { return new Date(element.CreatedDate); }), ), );` – Siddhartha Mukherjee Mar 31 '23 at 17:25

2 Answers2

2

Here is a better loop

let maxDate = 0;
let maxId = "";
data.forEach(({CreatedDate, ExperimentId}) => {
  const d = new Date(CreatedDate).getTime();
  if (d > maxDate) {
    maxId = ExperimentId;
    maxDate = d;
  }
})
console.log(new Date(maxDate), maxId)
<script>
data = [
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "PT1011",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2022-10-14T20:10:42.8276862+00:00"
        
    },
    {
        "ExperimentPlanningId": 20,
        "ExperimentId": "JD2994",
        "AnalystId": 2,
        "ExperimentTemplateId": 1,
        "NumberoFPools": null,
        "ExperimentStatus": "InProgress",
        "NextProcess": "234343",
        "CurrentProcess": "Test Process 1",
        "NextStage": "Stage 1 2",
        "CurrentStage": "Stage 1 2",
        "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
        "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
        "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
        "ProjectedOutcome": null,
        "ExperimentalDesign": null,
        "IsActive": true,
        "CreatedDate": "2023-03-31T16:23:19.5981913+00:00"
    }
]</script>

Reduce:

const maxId = data.reduce((savedMax, {CreatedDate, ExperimentId}) => {
  if (CreatedDate > savedMax.date) { // the date format is string comparable
    savedMax.date = CreatedDate;
    savedMax.id = ExperimentId;
  }
  return savedMax;
}, { date: "", id: ""}); // initialise savedMax
console.log(maxId);
<script>
  data = [{
      "ExperimentPlanningId": 20,
      "ExperimentId": "PT1011",
      "AnalystId": 2,
      "ExperimentTemplateId": 1,
      "NumberoFPools": null,
      "ExperimentStatus": "InProgress",
      "NextProcess": "234343",
      "CurrentProcess": "Test Process 1",
      "NextStage": "Stage 1 2",
      "CurrentStage": "Stage 1 2",
      "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
      "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
      "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
      "ProjectedOutcome": null,
      "ExperimentalDesign": null,
      "IsActive": true,
      "CreatedDate": "2022-10-14T20:10:42.8276862+00:00"

    },
    {
      "ExperimentPlanningId": 20,
      "ExperimentId": "JD2994",
      "AnalystId": 2,
      "ExperimentTemplateId": 1,
      "NumberoFPools": null,
      "ExperimentStatus": "InProgress",
      "NextProcess": "234343",
      "CurrentProcess": "Test Process 1",
      "NextStage": "Stage 1 2",
      "CurrentStage": "Stage 1 2",
      "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
      "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
      "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
      "ProjectedOutcome": null,
      "ExperimentalDesign": null,
      "IsActive": true,
      "CreatedDate": "2023-03-31T16:23:19.5981913+00:00"
    }
  ]
</script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • @Mike'Pomax'Kamermans So `max` is a postman thing? Sorry. I had no idea. – mplungjan Mar 31 '23 at 17:13
  • This is one of those jobs where on the JS side (rather than the API side), `reduce` actually makes sense. – Mike 'Pomax' Kamermans Mar 31 '23 at 17:13
  • I could do a reduce instead. Judging OP to be a beginner, my suggestion is more readable – mplungjan Mar 31 '23 at 17:14
  • 1
    Readability when it comes to `reduce` tends to depend on how well folks name their reduction variables. Most people pretend that `reduce((t,e) => someTernary, 0)` is somehow fine =) – Mike 'Pomax' Kamermans Mar 31 '23 at 17:14
  • Yea I am new to JS specifically. I am sure there are more efficient/shorthand methods to this but for my case this works fine. Thank you both for the tips – JD2775 Mar 31 '23 at 17:16
  • 1
    There's no accumulation here though, just a running max, so `currentMax` is probably a better name than `acc`. Add some spaces around the syntax (e.g. run it through `prettier`) and yeah would be a good example of something that's pretty readable. – Mike 'Pomax' Kamermans Mar 31 '23 at 23:04
-1

const jsonDataArray = [{
    "ExperimentPlanningId": 20,
    "ExperimentId": "PT1011",
    "AnalystId": 2,
    "ExperimentTemplateId": 1,
    "NumberoFPools": null,
    "ExperimentStatus": "InProgress",
    "NextProcess": "234343",
    "CurrentProcess": "Test Process 1",
    "NextStage": "Stage 1 2",
    "CurrentStage": "Stage 1 2",
    "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
    "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
    "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
    "ProjectedOutcome": null,
    "ExperimentalDesign": null,
    "IsActive": true,
    "CreatedDate": "2022-10-14T20:10:42.8276862+00:00"

  },
  {
    "ExperimentPlanningId": 20,
    "ExperimentId": "JD2994",
    "AnalystId": 2,
    "ExperimentTemplateId": 1,
    "NumberoFPools": null,
    "ExperimentStatus": "InProgress",
    "NextProcess": "234343",
    "CurrentProcess": "Test Process 1",
    "NextStage": "Stage 1 2",
    "CurrentStage": "Stage 1 2",
    "NextProcessStartDate": "2022-10-15T14:00:00+00:00",
    "CurrentProcessEndDate": "2022-10-15T14:00:00+00:00",
    "ExperimentStartDate": "2022-10-14T15:00:00+00:00",
    "ProjectedOutcome": null,
    "ExperimentalDesign": null,
    "IsActive": true,
    "CreatedDate": "2023-03-31T16:23:19.5981913+00:00"
  }
];

let newObject = {};
let dates = jsonDataArray.map(function(obj) {
  const regEx = new RegExp(/-/g);
  const dateKey = parseInt(obj.CreatedDate.replace(regEx, ""), 10)
  newObject[dateKey] = obj;
  return dateKey;
});

console.log("Max", newObject[Math.max(...dates)].CreatedDate);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Suhas Bachhav
  • 403
  • 1
  • 7
  • 28