I have an algorithm problem with JSON data to produce what I want.
I get some JSON data which I need to modify and display in client side with JS
JSON data:
[
{
"ISTNO": "0000",
"ATWCAN": "A",
"COLNAME": "A1"
},
{
"ISTNO": "0000",
"ATWCAN": "B",
"COLNAME": "A1"
},
{
"ISTNO": "0000",
"ATWCAN": "A",
"COLNAME": "A1"
},
{
"ISTNO": "0001",
"ATWCAN": "D33D",
"COLNAME": "B12"
},
{
"ISTNO": "0002",
"ATWCAN": "GG14",
"COLNAME": "C1C"
},
{
"ISTNO": "0003",
"ATWCAN": "FFF-13",
"COLNAME": "C13"
},
{
"ISTNO": "0004",
"ATWCAN": "F",
"COLNAME": "D1"
},
{
"ISTNO": "0004",
"ATWCAN": "ABB",
"COLNAME": "D1"
},
{
"ISTNO": "0004",
"ATWCAN": "AAA",
"COLNAME": "D1"
},
{
"ISTNO": "0004",
"ATWCAN": "AABA",
"COLNAME": "D1"
},
{
"ISTNO": "0005",
"ATWCAN": "",
"COLNAME": "E1"
},
{
"ISTNO": "0006",
"ATWCAN": "AGA",
"COLNAME": "G1"
},
{
"ISTNO": "0007",
"ATWCAN": "",
"COLNAME": "AFA"
},
{
"ISTNO": "0008",
"ATWCAN": "ABA",
"COLNAME": "FFFX"
},
{
"ISTNO": "0008",
"ATWCAN": "ATR",
"COLNAME": "FFFX"
},
{
"ISTNO": "0009",
"ATWCAN": "BAB",
"COLNAME": "BB"
},
{
"ISTNO": "0009",
"ATWCAN": "ABA",
"COLNAME": "BB"
},
{
"ISTNO": "0009",
"ATWCAN": "ABBA",
"COLNAME": "BB"
}
]
My desired result:
I need this table above so I need change data with JS
I tried to do it but it works wrong.
https://jsfiddle.net/ah04b2f9/6/
var aElements = [];
var maxRow = 4;
for (var i = 0; i < maxRow; i++) {
var oElement = [];
for (var t = 0; t < objData.length; t++) {
if (objData[t + 1] !== undefined) {
while (objData[t].ISTNO === objData[t + 1].ISTNO) {
if (objData[t].Flag !== "X") {
oElement[objData[t].COLNAME] = objData[t].ATWCAN;
}
objData[t + 1].Flag = "X";
objData.splice(t, 1);
t++;
if (objData[objData.length - 1] === objData[t]) {
break;
}
}
} else {
// oElement[objData[t].COLNAME] = objData[t].ATWCAN;
}
if (objData[t].Flag2 !== "X" && objData[t].Flag !== "X") {
oElement[objData[t].COLNAME] = objData[t].ATWCAN;
objData[t].Flag2 = "X";
} else {
//oElement[objData[t].COLNAME] = objData[t].ATWCAN;
}
}
for (var a1 = 0; a1 < objData.length; a1++) {
objData[a1].Flag = undefined;
}
aElements.push(oElement);
}
console.log(aElements);
}
question from:
https://stackoverflow.com/questions/65872964/how-to-change-json-data-dynamically-from-with-javascript