Add custom attributes to option


#1

Hi,

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:

Text

I am using this code:

$(id).select2({
	language: "es",
	$.map(respuesta, function (obj) {
		console.log(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.

Thanks


#2

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 codepen.io or jsfiddle.net, so I can see what’s going on.