You should be able to dynamically query an object type property using a variable as follow:
Objects.search().DesiredObjects().filter(obj => (obj as any)[desiredAttr].exactMatch(desiredValue));
Additionally, I find the function bellow can be useful to check that desiredAttr is valid:
import { UserFacingError } from "@foundry/functions-api";
import { ObjectType } from "@foundry/ontology-api";
export class Utils {
public static checkPropertyApiNameIsValid(propertyApiName: string, objectType: ObjectType): void {
const allPropertyApiNames = Object.values(objectType.properties).map(property => property.apiName);
if (!allPropertyApiNames.includes(propertyApiName)) {
throw new UserFacingError(`"${propertyApiName}" is not a valid property API name for object type ${objectType.apiName}. Valid names are: ${allPropertyApiNames.toString()}`);
}
}
}
And then you could call it just before doing the filtering:
function getAttribute(
desiredAttr: string,
desiredValue: string,
): Promise<DesiredObjectType> {
Utils.checkPropertyApiNameIsValid(desiredAttr, DesiredObjectType);
const desiredObjects = Objects.search().DesiredObjects().filter(obj => (obj as any)[desiredAttr].exactMatch(desiredValue));
return desiredObjects;
}