Is there any other better way to fill up the array like :
var arr = [];
var i = 0;
$('select').children('option').each( function() {
arr[i++] = $(this).html();
});
The fill() method fills specified elements in an array with a value. The fill() method overwrites the original array. Start and end position can be specified. If not, all elements will be filled.
Array.fill Using the Array. fill method is an obvious choice — you supply the method with the value you want to populate your array with, and the method returns a modified version of the array.
Array.prototype.fill() The fill() method changes all elements in an array to a static value, from a start index (default 0 ) to an end index (default array.length ). It returns the modified array.
You can use map
method:
var arr = $("select > option").map(function() {
return this.innerHTML;
}).get();
DEMO: http://jsfiddle.net/UZzd5/
using push()
:
var arr = [];
$('select').children('option').each( function() {
arr.push($(this).html());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With