codecowboy codecowboy - 1 year ago 114
Javascript Question

Javascript: How to create an array of object literals in a loop

I need to create an array of object literals like this:

var myColumnDefs = [
{key:"label", sortable:true, resizeable:true},
{key:"notes", sortable:true,resizeable:true},......

In a loop like this:

for ( var i=0 ; i < oFullResponse.results.length; i++) {

The value of
should be
in each element of the array.

Answer Source
var arr = [];
var len = oFullResponse.results.length;
for (var i = 0; i < len; i++) {
        key: oFullResponse.results[i].label,
        sortable: true,
        resizeable: true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download