循环遍历 response.Body 数组并检索特定元素 Id 和 DataFields.Value 的值的更简单方法 - 用于控制台日志
RESPONSE BODY - 这是在 Postman 中发送请求时返回的内容
[
{
"Id": "ae61098c-eb7c-4ee8-aca7-aa080112d656",
"DataFields": [
{
"Header": "LocationDetails.LocationID",
"Value": "BH127"
}
],
},
{
"Id": "84cd9a9f-d085-4642-b484-aa080112d656",
"DataFields": [
{
"Header": "LocationDetails.LocationID",
"Value": "BH128"
}
],
},
{
"Id": "6629d92b-1dcf-4fc1-a019-aa080112d656",
"DataFields": [
{
"Header": "LocationDetails.LocationID",
"Value": "BH129"
}
],
},
{
"Id": "47442b4f-f691-4213-b705-aa080112d656",
"DataFields": [
{
"Header": "LocationDetails.LocationID",
"Value": "BH130"
}
],
}
]
我的代码 - 测试选项卡中的 JavaScript
var logReportDataQryResults = JSON.parse(responseBody);
var locationOne = 0;
var locationTwo = 1;
var locationThree = 2;
var boreholeOne = logReportDataQryResults[locationOne].Id;
const objOne = logReportDataQryResults[locationOne].DataFields.find(({ Header }) => Header == 'LocationDetails.LocationID');
const holeOne = objOne ? objOne.Value : 'Not Found';
var boreholeTwo = logReportDataQryResults[locationTwo].Id;
const objTwo = logReportDataQryResults[locationTwo].DataFields.find(({ Header }) => Header == 'LocationDetails.LocationID');
const holeTwo = objTwo ? objTwo.Value : 'Not Found';
var boreholeThree = logReportDataQryResults[locationThree].Id;
const objThree = logReportDataQryResults[locationThree].DataFields.find(({ Header }) => Header == 'LocationDetails.LocationID');
const holeThree = objThree ? objThree.Value : 'Not Found';
我想让代码更有效率和可读性
最佳答案
您总是使用相同的模式(更改以 ALL_UPPERCASE 编写的部分):
var boreholeOne = logReportDataQryResults[ LOCATION ].Id;
const objOne = logReportDataQryResults[ LOCATION ].DataFields.find(({ Header }) => Header == 'LocationDetails.LocationID');
const holeOne = objOne ? objOne.Value : 'Not Found';
你可以将它封装在一个函数中,并将变化的部分传递给它:
const getBoreholeValue = (locationId, data) => {
const id = data[locationId].Id;
const obj = data[locationId].DataFields.find(({ Header }) => Header == 'LocationDetails.LocationID');
return obj ? obj.Value : 'Not Found';
}
然后像这样使用它:
const res1 = getBoreholeValue(0, logReportDataQryResults);
const res2 = getBoreholeValue(1, logReportDataQryResults);
const res3 = getBoreholeValue(2, logReportDataQryResults);
例子:
const respBody = [
{
"Id": "ae61098c-eb7c-4ee8-aca7-aa080112d656",
"DataFields": [
{
"Header": "LocationDetails.LocationID",
"Value": "BH127"
}
],
},
{
"Id": "84cd9a9f-d085-4642-b484-aa080112d656",
"DataFields": [
{
"Header": "LocationDetails.LocationID",
"Value": "BH128"
}
],
},
{
"Id": "6629d92b-1dcf-4fc1-a019-aa080112d656",
"DataFields": [
{
"Header": "LocationDetails.LocationID",
"Value": "BH129"
}
],
},
{
"Id": "47442b4f-f691-4213-b705-aa080112d656",
"DataFields": [
{
"Header": "LocationDetails.LocationID",
"Value": "BH130"
}
],
}
]
const getBoreholeValue = (locationId, data) => {
const id = data[locationId].Id;
const obj = data[locationId].DataFields.find(({ Header }) => Header == 'LocationDetails.LocationID');
return obj ? obj.Value : 'Not Found';
}
console.log(getBoreholeValue(0, respBody));
console.log(getBoreholeValue(1, respBody));
console.log(getBoreholeValue(2, respBody));
关于javascript - 从数组中检索多个值的更简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55588833/