King Kong
King Kong

Reputation: 2915

How to fill the array in jquery

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();
});

Upvotes: 11

Views: 20629

Answers (4)

mgraph
mgraph

Reputation: 15338

using push() :

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

Upvotes: 11

Lajos Mészáros
Lajos Mészáros

Reputation: 3871

Array.prototype.fill = function(value, length){
    while(length--){
        this[length] = value;
    }
    return this;
}

// example:
var coords = [].fill(0, 3);

Upvotes: 0

thecodeparadox
thecodeparadox

Reputation: 87073

var arr = [];
$('select option').html(function(i, html) {
    arr.push(html);
});

DEMO

Upvotes: 2

VisioN
VisioN

Reputation: 145388

You can use map method:

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

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

Upvotes: 14

Related Questions