我已经有以下格式:
[
{
user_id:"UID20",
first_name:"Vasanthis",
email:"vasanthi.g@redbeaninc.com"
},
{
user_id:"UID19",
first_name:"Murali",
email:"murali.m@redbeaninc.com"
}
];
我需要转换为以下格式:
[
{
"user_id":[
"UID20",
"UID19"
],
"first_name":[
"Vasanthis",
"Murali"
],
"email":[
"vasanthi.g@redbeaninc.com",
"murali.m@redbeaninc.com"
]
}
]
答案 0 :(得分:1)
您可以创建一个迭代数组并提取属性名和值的函数,并将它们放在一个新对象中,如下所示:
function arrObjToObjArr(a){
var newObj = {};
$.each(a, function(k,v) {
$.each(v, function(prop, val) {
(newObj[prop] == undefined) ? newObj[prop] = [val] : newObj[prop].push(val);
});
});
return newObj;
}
看到这个工作小提琴:https://jsfiddle.net/o56pmk38/