根据另一个字段中的最大日期查找JSON值。

huangapple go评论73阅读模式
英文:

Find JSON value based on Max date in another field

问题

Sure, here's the translated code snippet without the request for translation:

var jsonData = JSON.parse(responseBody);

var maxDate = null;
var experimentId = null;

for (var i = 0; i < jsonData.length; i++) {
  var createdDate = new Date(jsonData[i]["CreatedDate"]);

  if (maxDate === null || createdDate > maxDate) {
    maxDate = createdDate;
    experimentId = jsonData[i]["ExperimentId"];
  }
}

postman.setEnvironmentVariable("maxCreatedDateExperimentId", experimentId);

This code will find the maximum CreatedDate from your JSON response and store the associated ExperimentId in a Postman environment variable named maxCreatedDateExperimentId.

英文:

I have a JSON response body that looks like this:

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

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&lt;len; i++) {
  var value = max(jsonData[i][&quot;CreatedDate&quot;]);
}

答案1

得分: 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)

Reduce:
简化:

const maxId = data.reduce((savedMax, { CreatedDate, ExperimentId }) => {
  if (CreatedDate > savedMax.date) { // 日期格式可以进行字符串比较
    savedMax.date = CreatedDate;
    savedMax.id = ExperimentId;
  }
  return savedMax;
}, { date: "", id: "" }); // 初始化 savedMax
console.log(maxId);
英文:

Here is a better loop

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let maxDate = 0;
let maxId = &quot;&quot;;
data.forEach(({CreatedDate, ExperimentId}) =&gt; {
  const d = new Date(CreatedDate).getTime();
  if (d &gt; maxDate) {
    maxId = ExperimentId;
    maxDate = d;
  }
})
console.log(new Date(maxDate), maxId)

<!-- language: lang-html -->

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

<!-- end snippet -->

Reduce:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const maxId = data.reduce((savedMax, {CreatedDate, ExperimentId}) =&gt; {
  if (CreatedDate &gt; savedMax.date) { // the date format is string comparable
    savedMax.date = CreatedDate;
    savedMax.id = ExperimentId;
  }
  return savedMax;
}, { date: &quot;&quot;, id: &quot;&quot;}); // initialise savedMax
console.log(maxId);

<!-- language: lang-html -->

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

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

<!-- end snippet -->

答案2

得分: -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);

这是提供的 JavaScript 代码片段。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const jsonDataArray = [{
&quot;ExperimentPlanningId&quot;: 20,
&quot;ExperimentId&quot;: &quot;PT1011&quot;,
&quot;AnalystId&quot;: 2,
&quot;ExperimentTemplateId&quot;: 1,
&quot;NumberoFPools&quot;: null,
&quot;ExperimentStatus&quot;: &quot;InProgress&quot;,
&quot;NextProcess&quot;: &quot;234343&quot;,
&quot;CurrentProcess&quot;: &quot;Test Process 1&quot;,
&quot;NextStage&quot;: &quot;Stage 1 2&quot;,
&quot;CurrentStage&quot;: &quot;Stage 1 2&quot;,
&quot;NextProcessStartDate&quot;: &quot;2022-10-15T14:00:00+00:00&quot;,
&quot;CurrentProcessEndDate&quot;: &quot;2022-10-15T14:00:00+00:00&quot;,
&quot;ExperimentStartDate&quot;: &quot;2022-10-14T15:00:00+00:00&quot;,
&quot;ProjectedOutcome&quot;: null,
&quot;ExperimentalDesign&quot;: null,
&quot;IsActive&quot;: true,
&quot;CreatedDate&quot;: &quot;2022-10-14T20:10:42.8276862+00:00&quot;
},
{
&quot;ExperimentPlanningId&quot;: 20,
&quot;ExperimentId&quot;: &quot;JD2994&quot;,
&quot;AnalystId&quot;: 2,
&quot;ExperimentTemplateId&quot;: 1,
&quot;NumberoFPools&quot;: null,
&quot;ExperimentStatus&quot;: &quot;InProgress&quot;,
&quot;NextProcess&quot;: &quot;234343&quot;,
&quot;CurrentProcess&quot;: &quot;Test Process 1&quot;,
&quot;NextStage&quot;: &quot;Stage 1 2&quot;,
&quot;CurrentStage&quot;: &quot;Stage 1 2&quot;,
&quot;NextProcessStartDate&quot;: &quot;2022-10-15T14:00:00+00:00&quot;,
&quot;CurrentProcessEndDate&quot;: &quot;2022-10-15T14:00:00+00:00&quot;,
&quot;ExperimentStartDate&quot;: &quot;2022-10-14T15:00:00+00:00&quot;,
&quot;ProjectedOutcome&quot;: null,
&quot;ExperimentalDesign&quot;: null,
&quot;IsActive&quot;: true,
&quot;CreatedDate&quot;: &quot;2023-03-31T16:23:19.5981913+00:00&quot;
}
];
let newObject = {};
let dates = jsonDataArray.map(function(obj) {
const regEx = new RegExp(/-/g);
const dateKey = parseInt(obj.CreatedDate.replace(regEx, &quot;&quot;), 10)
newObject[dateKey] = obj;
return dateKey;
});
console.log(&quot;Max&quot;, newObject[Math.max(...dates)].CreatedDate);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月1日 01:02:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75901015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定