John St
John St

Reputation: 269

push assoc array into an array

How to assign object into an array within a loop?

My progress so far

item = [];

$.each(result, function(){
    //loop and assign array into an array
    //result is an object
    //result.a
    //result.b
});

Expected final result

item = [
    {'a': a},
    {'b': b },
    .....
]

Upvotes: 0

Views: 64

Answers (1)

Barmar
Barmar

Reputation: 780779

var item = [];

$.each(result, function(key, value) {
    var newobj = {};
    newobj[key] = value;
    item.push(newobj);
}

Upvotes: 1

Related Questions