猿问

js 处理 json

[{
"add":"4",
"shul":"2000",
"yanba":"20"
},{
"add":"5",
"shul":"2001",
"yanba":"21"
},{
"add":"6",
"shul":"2002",
"yanba":"22"
}]
如上json想要处理成这样的格式,也就是把相同的属性对应的值放到一个数组里面。这里的属性名未知。
{
“add”:["4","5","6"],
"shul":["2000","2001","2002"],
"yanba":["20","21","22"],
}
回首忆惘然
浏览 355回答 2
2回答

料青山看我应如是

vardata=[{"add":"4","shul":"2000","yanba":"20"},{"add":"5","shul":"2001","yanba":"21"},{"add":"6","shul":"2002","yanba":"22"}],obj={};data.forEach(function(d){for(varkind){if(!obj[k])obj[k]=[];obj[k].push(d[k]);}})

德玛西亚99

javascriptfunctiontrans(arr){varresult={};arr.forEach(function(val){for(variinval){if(typeofresult[i]=="undefined")result[i]=[];result[i].push(val[i])}})returnresult;}简单循环处理一下就可以了。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答