Add custom attributes to option



There is something I can’t solve.

I am loading the data from a JSON array into select2, and I would like to add some custom attributes, for example:


I am using this code:

	language: "es",
	$.map(respuesta, function (obj) {
		return {
			id: obj.id_cliente, 
			text: obj.nombre,
			actividad: obj.actividad,

However, just add id and text.

Please, can you help me?

I know I can add the options with a foreach, but it is much slower when using DOM.



Your code sample appears to be incomplete. Where is the JSON array? Your $.map() call seems like incorrect syntax since its value is not assigned to a key (probably the “data” key) in the initializing object.

Please post your entire code, either here or on a site like or, so I can see what’s going on.