Search code examples
javascriptjquerybootstrap-typeahead

Object literal in filter function


I have my filter configured like this,and I need to pass the filter object in a dinamic way.

filter: function ( response ) {
    return $.map(response,function (object) {                   
        return {
            value: object.descricaoMunicipio,
            municipioId: object.municipioId,
            descricaoMunicipio: object.descricaoMunicipio,
            id: object.municipioId
        };
    });     
}

I need to pass the return in a generic way,like this:Is it possible?

var fnc = {
    value: object.uf.descricaoUf,
    municipioId: object.municipioId,
    descricaoMunicipio: object.uf.descricaoUf,
    id: object.municipioId
}


filter: function ( response ) {
    return $.map(response,function (object) {
        return {
            eval(fnc)
        };
    });
}

Solution

  • You can define a named that does what you want:

    var fnc = function(object) {
        return {
            value: object.uf.descricaoUf,
            municipioId: object.municipioId,
            descricaoMunicipio: object.uf.descricaoUf,
            id: object.municipioId
        };
    }
    

    And then use it like this:

    filter: function(response) {
        return $.map(response, function (object) {
            return fnc(object);
        });
    }
    

    Or you can just write:

    filter: function(response) {
        return $.map(response, fnc);
    }