How to use .where formatter with nested objects

Thanks to @Appleโ€™s suggestion that perhaps the .where formatter does not support nesting or dot notation, I let chatgpt create a custom formatter that does.

And now it correctly retrieves the right object based on a value in a nested object! :tada: :tada: :tada: :tada:

Place this in your extensions/server_connect/formatters/somefile.js

// Custom formatter to filter array based on nested property, operator and value
exports.whereWithDot = function (arr, path, operator, value) {
    // Function to get nested property value using dot notation
    function getNestedValue(obj, path) {
        var keys = path.split('.');
        var current = obj;
        for (var i = 0; i < keys.length; i++) {
            if (current[keys[i]] !== undefined) {
                current = current[keys[i]];
            } else {
                return undefined;
            }
        }
        return current;
    }

    // Function to compare values based on operator
    function compare(val1, operator, val2) {
        switch (operator) {
            case '==':
                return val1 == val2;
            case '!=':
                return val1 != val2;
            case '>':
                return val1 > val2;
            case '>=':
                return val1 >= val2;
            case '<':
                return val1 < val2;
            case '<=':
                return val1 <= val2;
            default:
                throw new Error('Invalid operator: ' + operator);
        }
    }

    // Filter the array
    return arr.filter(function(item) {
        var nestedValue = getNestedValue(item, path);
        return compare(nestedValue, operator, value);
    });
};

And now this

{{list.whereWithDot('instance.number', '==', 20338)}}

Returns the proper object!