如何在javascript中使用循环创建关联数组

时间:2016-05-27 09:49:46

标签: javascript ajax

//response = [{"id":"1","created":"26-05-2016"},{"id":"2","created":"25-05-2016"},{"id":"8","created":"21-05-2016"}]

success : function(response){
var obj = JSON.parse(response);

//这里我想创建关联数组...就像array['user'] = id& array['created'] ..

请帮帮我

1 个答案:

答案 0 :(得分:2)

只需尝试这个



    var response = [{"id":"1","created":"26-05-2016"},{"id":"2","created":"25-05-2016"},{"id":"8","created":"21-05-2016"}];
    var output = response.map(function(obj){ return { user: obj.id, created: obj.created } })
    
    console.log(output)