Effective object set filtering in functions

I have an object set with about 2k object. I have an array of key_name (string) & date (LocalDate) object pairs. I want to filter the object set where both the key_name & date are a match (i.e object_type.date === date && object_type.key_name === key_name). what is the best/most efficient way to do this? (the downstream workflow is, i have an array where each element represents an object edit i will need to do, like which values to edit. i need to pass the object, with the values, to an edit helper function).

The key_name & date pairs look like:

const dateKeyNameSet = new Set(
                processedDataArray.map(item => `${item.date}-${item.key_name}`)
);
const dateKeyNameArray = Array.from(dateKeyNameSet).map(pair => {
                const [date, key_name] = pair.split('-');
                return { date, key_name };
            });

Hi, is it possible to directly create a string composite key on object_type called dateKey and do object_set.filter(obj=>obj.dateKey.exactMatch(...dateKeyNameSet)) where dateKeyNameSet is the same dateKeyNameSet in your provided code snippet?

1 Like

To clarify it sounds like you want to, for each (date, keyName) tuple in the array, match on all objects that contain that tuple. @yushi’s suggestion would work, or you can also construct these filters without creating additional properties.

In that case you can create an OR filter over each tuple, and an AND filter within each tuple.

Something like:

// Just declaring a variable, not actually constructing it as I'm not sure
// what types you have/need.
declare const dateKeyNames: Array<{ date: LocalDate, keyName: string }>;

const os = Objects.search().yourObjectType().filter(o => Filters.or(...dateKeyNames.map(({ date, keyName }) => Filters.and(o.dateProperty.exactMatch(date), o.keyProperty.exactMatch(keyName)))));
3 Likes