我有json喜欢
SampJson= [
{
data: "50",
cIndex: "column0",rowno:"1"
},
{
data: "5",
cIndex: "column1",rowno:"1"
},
{
data: "80",
cIndex: "column0",rowno:"2"
},
{
data: "65",
cIndex: "column1",rowno:"2"
}
];
我需要创建一个像
这样的JSON[
{
rowno:1,
column0:50,
column1:5
},
{
rowno:2,
column0:80,
column1:65
}
];
提前致谢...
答案 0 :(得分:1)
你应该这样做:
sampJson = [
{
data: "50",
cIndex: "column0",
rowno: "1"
},
{
data: "5",
cIndex: "column1",
rowno: "1"
},
{
data: "80",
cIndex: "column0",
rowno: "2"
},
{
data: "65",
cIndex: "column1",
rowno: "2"
}
];
var results = [];
sampJson.forEach(function(el) {
if (results[+el.rowno - 1] == null) {
results[+el.rowno - 1] = new Object();
}
results[+el.rowno - 1].rowno = +el.rowno;
results[+el.rowno - 1][el.cIndex] = +el.data;
});
console.log(results);