I’m dealing with strange situation here..
I’ve an array something like below
var gblCalibrationData = [
{
"CalibrationYear": "2012",
"CalibrationData": [
{
"Axis": "Profile",
"P1": "2",
"P2": "4",
"P3": "2",
"P4": "4",
"P5": "2",
"P6": "3",
"P7": "4",
"P8": "5",
"P9": "5",
"P10": "5",
},
{
"Axis": "Symmetry",
"P1": "3",
"P2": "4",
"P3": "3",
"P4": "2",
"P5": "3",
"P6": "4",
"P7": "5",
"P8": "5",
"P9": "6",
"P10": "6",
},
{
"Axis": "Error (%)",
"P1": "6",
"P2": "7",
"P3": "5",
"P4": "4",
"P5": "4",
"P6": "5",
"P7": "6",
"P8": "7",
"P9": "7",
"P10": "8",
},
{
"Axis": "Velocity (m/s)",
"P1": "5",
"P2": "7",
"P3": "6",
"P4": "4",
"P5": "3",
"P6": "45",
"P7": "7",
"P8": "7",
"P9": "7",
"P10": "7",
}
],
"CalibrationYear": "2022",
"CalibrationData": [
{
"Axis": "Profile",
"P1": "2",
"P2": "4",
"P3": "2",
"P4": "4",
"P5": "2",
"P6": "3",
"P7": "4",
"P8": "5",
"P9": "5",
"P10": "5",
},
{
"Axis": "Symmetry",
"P1": "3",
"P2": "4",
"P3": "3",
"P4": "2",
"P5": "3",
"P6": "4",
"P7": "5",
"P8": "5",
"P9": "6",
"P10": "6",
},
{
"Axis": "Error (%)",
"P1": "6",
"P2": "7",
"P3": "5",
"P4": "4",
"P5": "4",
"P6": "5",
"P7": "6",
"P8": "7",
"P9": "7",
"P10": "8",
},
{
"Axis": "Velocity (m/s)",
"P1": "5",
"P2": "7",
"P3": "6",
"P4": "4",
"P5": "3",
"P6": "45",
"P7": "7",
"P8": "7",
"P9": "7",
"P10": "7",
}
]
}
]
I need to convert this array to pivot it with CalibrationData appending first letter of "Axis"
and year
something like below
var CalibrationData = [
{
"2012p": "2",
"2012s": "3",
"2012e": "6",
"2012v": "5"
},
{
"2012p": "2",
"2012s": "3",
"2012e": "6",
"2012v": "5"
},
{
"2012p": "2",
"2012s": "3",
"2012e": "6",
"2012v": "5"
},
{
"2012p": "2",
"2012s": "3",
"2012e": "6",
"2012v": "5"
},
{
"2012p": "2",
"2012s": "3",
"2012e": "6",
"2012v": "5"
},
{
"2012p": "2",
"2012s": "3",
"2012e": "6",
"2012v": "5"
},
{
"2012p": "2",
"2012s": "3",
"2012e": "6",
"2012v": "5"
},
{
"2012p": "2",
"2012s": "3",
"2012e": "6",
"2012v": "5"
},
{
"2012p": "2",
"2012s": "3",
"2012e": "6",
"2012v": "5"
},
{
"2022p": "2",
"2022s": "3",
"2022e": "6",
"2022v": "5"
},
{
"2022p": "2",
"2022s": "3",
"2022e": "6",
"2022v": "5"
},
{
"2022p": "2",
"2022s": "3",
"2022e": "6",
"2022v": "5"
},
{
"2022p": "2",
"2022s": "3",
"2022e": "6",
"2022v": "5"
},
{
"2022p": "2",
"2022s": "3",
"2022e": "6",
"2022v": "5"
},
{
"2022p": "2",
"2022s": "3",
"2022e": "6",
"2022v": "5"
},
{
"2022p": "2",
"2022s": "3",
"2022e": "6",
"2022v": "5"
},
{
"2022p": "2",
"2022s": "3",
"2022e": "6",
"2022v": "5"
}
]
basically, in expected form, 2012 or 2022 is the CalibrationYear and p,s,e,v are first letter or Profile, Symmetry, Error and Velocity respectively replacing P1, P2 in every object.
Any hints… 🙁
2
Answers
well, I found it ..
still, will appreciate if any better way to do it...
Just loop or even nested loops. Nothing fancy. I added some comments in code. This can be shortened using one big
Array.reduce
but if it works and readable, might as well leave it as is.