Simpler way to retrieve multiple values from array

how to vlookup and return multiple corresponding values vertically in excel
index match return multiple values vertically
excel index match return multiple values in one cell
index match return multiple values horizontally
extract multiple matches into separate rows
vlookup return multiple values in one cell separated by comma
return all rows that match criteria excel
excel return array based on criteria

A simpler way to loop through response.Body array and retrieve values of specific elements Id and DataFields.Value - to be used in console log

RESPONSE BODY - this is what is returned when request sent in 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"
            }
        ],
    }
]

MY CODE - JavaScript in Tests tab

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';

I want to make code more efficient and readable

You are always using the same pattern (changing parts written in ALL_UPPERCASE):

var boreholeOne = logReportDataQryResults[ LOCATION ].Id;
const objOne = logReportDataQryResults[ LOCATION ].DataFields.find(({ Header }) => Header == 'LocationDetails.LocationID');
const holeOne = objOne ? objOne.Value : 'Not Found';

You can encapsulate it in a function and pass the changing parts into it:

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';
}

And then use it like this:

const res1 = getBoreholeValue(0, logReportDataQryResults);
const res2 = getBoreholeValue(1, logReportDataQryResults);
const res3 = getBoreholeValue(2, logReportDataQryResults);

Example:

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));

5 easy ways to VLOOKUP and return multiple values, But there is a workaround to identify multiple matches. The array formulas demonstrated below are smaller and easier to understand and� Retrieving the number of array elements in a simple array can most easily be done by using the CARDINALITY function and retrieving the maximum allowed size of an array can be done using the MAX_CARDINALITY function.

You might use a single object instead of lots of separate variables - use .map to iterate over the locations and construct a new array with the info you need:

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);

Return Multiple Match Values in Excel - Xelplus, Quickly learn how to return multiple match results in Excel. You'll learn 2 methods . Excel 2016, Office 365. It's quite simple to setup but it's an array formula that requires CSE. Excel get multiple match results by looking up one value in Excel� Retrieving the number of array elements in a simple array can most easily be done by using the CARDINALITY function and retrieving the maximum allowed size of an array can be done using the MAX_CARDINALITY function.

    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) 

5 ways to Return multiple values from a method in Java, In this post, we will see how to return multiple values from a function in Java using a POJO Below is simpler version of above program that uses an Object array to store all the System.out.println("Name field has value " + details.get(0)); . Sub Strore_Retrieve_Array() Dim My_Array() As String Dim WS As Worksheet Set WS = ActiveSheet '<< Storing the Values in an Array >> For X = 2 To 13 ReDim Preserve My_Array(X - 2) ' Storing from Index(0) My_Array(X - 2) = Cells(X, 1).Value Next X '<< Retrieving the Values from an Array >> For K = LBound(My_Array()) To UBound(My_Array()) Msgbox My_Array(K)

Returning Multiple Values from a Function, Returning multiple values from a function using an array The following shows how to get the return value from the getNames() function: name to each returned value to make it more readable and easier to maintain, you can use an object:. We need to get the index number of each occurrence of value, then retrieve values using the INDEX function of Excel. Hence the main challenge is of getting an array of index numbers of lookup_value. To get index numbers, we used IF and ROW functions. The formula is indeed complex altogether, let’s break it down.

How to Lookup Multiple Instances of a Value in Excel, Vlookup to retrieve multiple values from a list. Using INDEX an array formula. After writing formula hit CTRL+SHIFT+ENTER key to make it an array formula. Even though the formula may look complex, but the idea is simple. We need to get� The lookup formula retrieves data from multiple columns of the data table at one time. To do this, the lookup formula must reside in all of the fields from which you want information. To retrieve data from columns 2, 3, and 4 of the data table (the price, the part number, and the supplier's name), enter a partial name as the Lookup_value.

How to Return Multiple Values From a Java Method, In this tutorial, we'll learn different ways to return multiple values from a Java method. For example, the following getCoordinates method returns an array of two double keys with the Map#get method to retrieve the corresponding value. By doing so, we create simple, thread-safe, sharable objects. To retrieve multiple matching values from a set of data with a formula, you can use the IF and SMALL functions to figure out the row number of each match and feed that value back to INDEX. In the example shown, the formula in I7 is: { = INDEX ( amts

Comments
  • Are you trying to get the Value to the item with Header "LocationDetails.LocationID" and do that for the first three objects in this array?
  • @VLAZ, yes I am
  • Sorry, didn't notice your answer was first. And it is better than mine, therefor => upvote! :)