问题
I know there are threads on this, but I dont get it and might need help on my json file. I want to bind a json to one of my SAPUI5 controls, but I dont get it converted.
The thing is, I got my data already in the frontend, so I dont want to make a second backend call and I need to transpose my json data.
This is what I got, columns are Mon01 .. etc.
Mon01 Mon02 Mon03 Mon04
0 03/2015 04/2015 05/2015 06/2015
1 1 3 5 21
2 2 4 6 22
3 10 11 12 23
This is what I want:
Mon01 Mon02 Mon03 Mon04
0 03/2015 1 2 10
1 04/2015 3 4 11
2 05/2015 5 6 12
3 06/2015 21 22 23
Or in JSON what I got:
d :
[
0: {
Mon01: "03/2015",
Mon02: "04/2015",
Mon03: "05/2015",
},
1: {
Mon01: "1,0",
Mon02: "3,0",
Mon03: "5,0",
},
2: {
Mon01: "2,0",
Mon02: "4,0",
Mon03: "6,0",
Mon04: "",
Mon05: "",
Mon06: "",
},
3: {
Mon01: "10,0",
Mon02: "11,0",
Mon03: "12,0",
Mon04: "",
Mon05: "",
Mon06: "",
},
length: 3
]
and in JSON what I want:
d :
[
0: {
Mon01: "03/2015",
Mon02: "1,0",
Mon03: "2,0",
Mon04: "3,0",
Mon05: "",
Mon06: "",
},
1: {
Mon01: "04/2015",
Mon02: "3,0",
Mon03: "4,0",
Mon04: "11,0",
Mon05: "",
Mon06: "",
},
2: {
Mon01: "05/2015",
Mon02: "5,0",
Mon03: "6,0",
Mon04: "12,0",
Mon05: "",
Mon06: "",
},
length: 3
]
What I've done which is not pretty the best way (but working) a single method moving each line:
transpose : function(oTreeAll) {
var oTreeNew = new Array();
var oTreeSingle = {};
oTreeSingle.Mon01 = oTreeAll[0].Mon01.replace(',', '.');
oTreeSingle.Mon02 = oTreeAll[1].Mon01.replace(',', '.');
oTreeSingle.Mon03 = oTreeAll[2].Mon01.replace(',', '.');
oTreeSingle.Mon04 = oTreeAll[3].Mon01.replace(',', '.');
oTreeNew.push(oTreeSingle);
var oTreeSingle1 = {};
oTreeSingle1.Mon01 = oTreeAll[0].Mon02.replace(',', '.');
oTreeSingle1.Mon02 = oTreeAll[1].Mon02.replace(',', '.');
oTreeSingle1.Mon03 = oTreeAll[2].Mon02.replace(',', '.');
oTreeSingle1.Mon04 = oTreeAll[3].Mon02.replace(',', '.');
oTreeNew.push(oTreeSingle1);
var oTreeSingle2 = {};
oTreeSingle2.Mon01 = oTreeAll[0].Mon03.replace(',', '.');
oTreeSingle2.Mon02 = oTreeAll[1].Mon03.replace(',', '.');
oTreeSingle2.Mon03 = oTreeAll[2].Mon03.replace(',', '.');
oTreeSingle2.Mon04 = oTreeAll[3].Mon03.replace(',', '.');
oTreeNew.push(oTreeSingle2);
var data = {
"d" : {
"results" : oTreeNew,
}
};
return data;
},
How would you do it? I've read a lot about map functions, but I dont understand the way the work or how to adapt it to my json, can anyone help me with it? THANKS ALOT!
Thanks to Shibaji Chakraborty, I got the solution for my json:
transpose : function(oTreeAll) {
var keys = [];
var i = 0;
for ( var key in oTreeAll[i]) {
keys.push(key);
}
console.log(keys);
var newObj = {
d : []
};
newObj['length'] = oTreeAll.length;
for (var k = 0; k < oTreeAll.length; k++) {
var obj = {};
for ( var cnt in keys) {
obj[keys[cnt]] = "";
}
newObj.d.push(obj);
}
for (var k = 0; k < oTreeAll.length; k++) {
for (var j = 0; j < oTreeAll.length; j++) {
newObj.d[k][keys[j]] = oTreeAll[j][keys[k]];
}
}
console.log(newObj);
return newObj;
},
回答1:
Please find the code below:
var data = {d :
[
{
Mon01: "03/2015",
Mon02: "04/2015",
Mon03: "05/2015",
},
{
Mon01: "1,0",
Mon02: "3,0",
Mon03: "5,0",
},
{
Mon01: "2,0",
Mon02: "4,0",
Mon03: "6,0",
Mon04: "",
Mon05: "",
Mon06: "",
},
{
Mon01: "10,0",
Mon02: "11,0",
Mon03: "12,0",
Mon04: "",
Mon05: "",
Mon06: "",
}
],
length: 3};
//console.log(data.d[0]);
var keys = [];
for(var key in data.d[data.length]){
//console.log(key);
keys.push(key);
}
var newObj = {d:[]};
newObj['length'] = data.length;
for(var k =0;k<data.length;k++){
var obj = {};
for(var cnt in keys){
obj[keys[cnt]] = "";
}
newObj.d.push(obj);
}
for(var k =0;k<data.length;k++){
//var obj = {};
//console.log(k);
for(var j=0;j<data.length;j++){
newObj.d[k][keys[j]] = data.d[j][keys[k]];
}
}
console.log(newObj);
来源:https://stackoverflow.com/questions/29081454/transposing-json-data