Skip to content
Advertisement

Google Scripts – Combine multiple arrays in vlookup type fashion

I am trying to get an array to print out information from two different sources. I am looking for something that will look up one set of values, print the array. then take a value from the first array, use this in a vlookup type fashion, find the value in the second array, and then create a new array that has all the values I need.

So take this code below. I am grabbing the fields I care about in the ‘contents’ variable, and printing it to an array oA. But in ‘contents2’ there is another value that I also care about, which I am currently printing to oA2. Based on what I’ve done so far I have two different arrays, that I basically need to append together. Since the first array could be 10 items or so, and the list on contents2 could be 100 items, I’m looking for a way to take the value I’ve seen in oA and use that as a vlookup value in contents2 and print that in a new array.

JavaScript

This will produce the following:

JavaScript

But I really want it to produce:

JavaScript

Any pointers on the best way to accomplish this?

Advertisement

Answer

Solution:

You can use bracket notation to reference a property using a variable. Note that in the sample code I changed the NMR balance to 1 so it will be displayed as well.

JavaScript

Sample Result:

enter image description here

References:

Property Accessors

Addendum:

If you want to use the new format in your comment, you need to check each obj2.base like this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement