从数组检索多个值的更简单方法

时间:2019-04-09 08:57:07

标签: javascript arrays postman

一种遍历response.Body数组并检索特定元素Id和DataFields.Value的值的更简单方法-在控制台日志中使用

响应身体-这是在邮递员中发送请求时返回的内容

[
    {
        "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';

我想使代码更高效和可读性

3 个答案:

答案 0 :(得分:1)

您可以使用单个对象而不是许多单独的变量-使用.map遍历位置并使用所需信息构造新数组:

const logReportDataQryResults = [
    {
        "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 locations = [0, 1, 2];
const boreholes = locations.map((location) => {
  const { Id, DataFields } = logReportDataQryResults[location];
  const obj = DataFields.find(({ Header }) => Header == 'LocationDetails.LocationID');
  const holeValue = obj ? obj.Value : 'Not Found';
  return { Id, obj, holeValue };
});
console.log(boreholes);

答案 1 :(得分:1)

您始终使用相同的模式(更改用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));

答案 2 :(得分:0)

    var logReportDataQryResults = 
        [
        {
            "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 boreholes=[];

    for (key in logReportDataQryResults) {

        const { Id, DataFields } = logReportDataQryResults[key];
        const obj = DataFields.find(({ Header }) => Header == 'LocationDetails.LocationID');
        const holeValue = obj ? obj.Value : 'Not Found';
        boreholes.push({ "Id": Id,"holeValue": holeValue });
    }
    console.log(boreholes)