Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push to array a key name taken from variable

I have an array:

var pages = new Array();

I want to push my pages data to this array like this:

$('li.page').each(function () {
        var datatype = $(this).attr('data-type');
        var info = $(this).attr('data-info');
        pages_order.push({datatype:info});
    });

but this code doesn't replace datatype as variable, just puts datatype string as a key. How do I make it place there actual string value as a key name?

like image 493
Sergei Basharov Avatar asked Dec 10 '22 01:12

Sergei Basharov


1 Answers

I finally saw what you were trying to do:

var pages = new Array();
$('li.page').each(function () {
    var datatype = $(this).attr('data-type');
    var info = $(this).attr('data-info');
    var temp = {};
    temp[datatype] = info;
    pages_order.push(temp);
});
like image 131
nathanjosiah Avatar answered Dec 26 '22 19:12

nathanjosiah