将assoc数组推入数组

时间:2015-01-09 02:14:09

标签: javascript jquery

如何将对象分配到循环中的数组中?

到目前为止我的进展

item = [];

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

预期的最终结果

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

1 个答案:

答案 0 :(得分:1)

var item = [];

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