1

I have the following code which is part of a autocomplete ajax query. My code returns a JSON it's working fine with the attached code, however I would like to use a variable in place of the item*.d_name* ie. I would like to make the auto complete function become more a boilerplate solution in which I can pass a variable to this function and it would substitute the item*.d_name*, with the variable's value. I am having difficulty in trying to figure out how to do this.

success: function(data)  {
        json = $.parseJSON(data);

        response($.map(json, function(item) {
            return {
                label:  item.d_name ,
                value:  item.d_name

            };
        }));
        },
  • If accessing an object property with a dynamic name is what you're stuck on then the linked duplicate covers it. If figuring out how to pass a variable into the Ajax success handler is what you're stuck on then please leave a comment about that and/or [edit] your question to provide more details. – nnnnnn Jul 22 '16 at 02:51
  • Thanks, it was being able to use a dynamic name, which is solved now appreciate your help – Lawrence Edel Jul 22 '16 at 02:54

1 Answers1

1

you can use this way,

var KEY = 'd_name'; // declare in somewhere or function param

return {
  label:  item[KEY],
  value:  item[KEY]
};
Alongkorn
  • 3,968
  • 1
  • 24
  • 41