How to fill an array in jquery

Is there any other better way to populate an array, for example:

var arr = []; var i = 0; $('select').children('option').each( function() { arr[i++] = $(this).html(); }); 
+7
source share
4 answers

You can use the map method:

 var arr = $("select > option").map(function() { return this.innerHTML; }).get(); 

DEMO: http://jsfiddle.net/UZzd5/

+12
source

using push() :

 var arr = []; $('select').children('option').each( function() { arr.push($(this).html()); }); 
+7
source
 var arr = []; $('select option').html(function(i, html) { arr.push(html); }); 

Demo

+2
source
 Array.prototype.fill = function(value, length){ while(length--){ this[length] = value; } return this; } // example: var coords = [].fill(0, 3); 
0
source

All Articles