I donβt think there is a built-in way to create an array as you want. But you can simplify the code a bit with $().map() :
var attributes = $({selector}).map( function( i, element ) { return $(element).data( 'myAttribute' ); });
Or, if this is something that can be used in more than one place, you can make it a plugin:
$.fn.dataArray = function( name ) { return this.map( function( i, element ) { return $(element).data( name ); }); };
and name it with this very simple code:
var attributes = $({selector}).dataArray( 'myAttribute' );
source share