Most efficient way to check if field is in another model

What’s the most efficient way to check if a field is in another model in Javascript? I have this situation come up many times, usually during a field render in a list view. 

Here’s one situation:
Product list view

  • Editable list of Products
  • Want to show a “Total quantity owned” field (can be Ui only) that is really an aggregate of a stock object
What I’m currently doing:
  • Create aggregate model on Stock grouping by Product, summing Quantity
  • Create JS field render that does a for loop on my aggStock model
  • Compare the field being rendered to each field in aggStock, if the Id’s match up, pull the aggQuantity field and append it using a field render. If no matches are found, put a 0 for quantity.
  • Apply custom field render on my quantity field
This does work. But it seems terribly inefficient that for each field in my list, I am cycling through every field in another model and comparing the Ids. 

Would it be better to:
  • Have a condition on my aggStock model where Product Id = null and set that condition value in the field render using JS?
  • Create an array of all of the Ids in my aggStock model by looping all of the fields, then using .indexOf to find that specific row’s position in the array and pull info from there?
  • Is there something glaringly obvious that I’m missing?

If you are trying to simply show the global total, and are not happy with the Table Total view you will have to go to JS.  The means you describe seems valid,  but I guess I’m not sure why you are comparing the ID’s before you are summing the summedQuantity from your Agg model. 

You might be able to get what you are after with the Lookup function in our new formula fields.  The syntax is a little tricky.  Here is an example: 

ModelA,   QuantityField,  CategoryField
ModelB, CategoryField

Add UI Only field to ModelB use Lookup formula:   Lookup(“ModelA”,“QuantityField”, “CategoryField”, {{CategoryField}})  
Quotes and braces are important. 

Thanks for the response Rob. I’m not after a global total, its more of a sum per line item in a list view table. If I have a list of all of my products in inventory. In this table view, I want to show a quantity owned of each item. This is looking up to a stock ticket object. My agg model is based on stock and grouped by product Id. This is why I am comparing the Ids between the agg model and line item being rendered to pull the correct quantity.

That being said, shortly after posting this, I discovered the Ui-Only lookups and they have been working great! Very happy with them and find them incredibly useful. I guess my question now is more of a resources question. What is the most efficient (in terms of quickest results) to get this information. Is the Ui-Only reference field better than using a ‘for’ loop and better than using an $.each query? Want to make sure I’m making things scalable as more and more records are added and searched.