Help

Save the date! Join us on October 16 for our Product Ops launch event. Register here.

Return an Array of Values

1685 1
cancel
Showing results for 
Search instead for 
Did you mean: 
Britt_Givens
4 - Data Explorer
4 - Data Explorer

Hi - I am new to Script, hoping someone can help me have this function return an array or values rather then just one.

EX) Zip code 10001 exist in multiple cells in “ZIP” and was hoping to return all values associated with 10001, not just one value.

//Substitute “Us Enterprise” for table name which contains values

//on which you want to run the vlookup

let mainTable = base.getTable(“US Enterprise”);

let mainTableRecords = await mainTable.selectRecordsAsync();

//Substitute “ZipCodeData” for table which contains range to search in

let lookupTable = base.getTable(“ZipCodeData”);

let lookupRangeRecords = await lookupTable.selectRecordsAsync();

//Replace "Zip Code " with column name which has the values you want to look up

for (let record of mainTableRecords.records) {

 let lookupValue = record.getCellValue("Zip Code");

//Replace "Name" with columnn name which value should be returned

for (let rangeRecord of lookupRangeRecords.records) {

    if (rangeRecord.getCellValue("Zip") === lookupValue) {

        let returnValue = rangeRecord.getCellValue("Name");

 

        //Replace "Carrier" with update value

        await mainTable.updateRecordAsync(record, {

            "Carrier": returnValue

        });

   }

}

}

1 Reply 1

Welcome to the community, @Britt_Givens! :grinning_face_with_big_eyes: I recommend finding a good JavaScript reference and playing a bit on your own if you’re interested in learning scripting. To adapt this script based on what you described will require only a few additions:

  • Before the main loop, you’d add a line that creates an empty array
  • Instead of assigning the found cell value to returnValue, you would append the value to this array using the .push() method.
  • The lines that update the main table record would be moved after the for...of loop, and only execute if the loop ended with a non-empty array.
  • If the update executes, you would use the .join() method on the array to convert it into a comma-separated item string to insert into the desired field.

If you need more guidance after taking a crack at it on your own, just holler!