Ajax Pre-select only if result datas length is 1

Hello everyone, first time here. I have read all the docs and cant figure out how to achieve the following behavior. I want to add a new row to a table if and only if the result from my ajax is 1meaning the result returned one item. This is what I got so far, but I don’t know if this it the best way.

$("#short_products").select2({
    placeholder: "search products",
    minimumInputLength: 3,
    ajax: {
        delay: 250,
        url: function (params) {
            return "/search";
        },
        data: function (params) {
            return {
                q: params.term
            };
        },
        processResults: function (data) {
            if (data.results.length == 1) {
                $(".select2-search__field").val('');

                table.row.add([
                        counter,
                        data.results[0].id,
                        data.results[0].code,
                        data.results[0].text,
                        1
                ]).draw(false);
                
                return { results: [ { "id": 1, "text": "Added to table" }] };
            }
            return data;
        },
        cache: true
    }
});

The code above works but I dont know if this is the best way to achieve this behavior. I wasnt able to pre-select a value so this the code above was the close I could get to what I wanted.