skip to Main Content

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


  1. Chosen as BEST ANSWER

    well, I found it ..

    var SrtData = [];
            for (var s = 0; s < gblClibrationData.length; s++) {
                for (var ss = 1; ss < 11; ss++) {
                    var data = {};
                    for (var t = 0; t < gblClibrationData[s].CalibrationData.length; t++) {
                        if (gblClibrationData[s].CalibrationData[t].Axis.includes("Profile")) {
                            data[gblClibrationData[s].CalibrationYear + "p"] = gblClibrationData[s].CalibrationData[t]['P' + ss];
                        }
                        if (gblClibrationData[s].CalibrationData[t].Axis.includes("Symmetry")) {
                            data[gblClibrationData[s].CalibrationYear + "s"] = gblClibrationData[s].CalibrationData[t]['P' + ss];
                        }
                        if (gblClibrationData[s].CalibrationData[t].Axis.includes("Error")) {
                            data[gblClibrationData[s].CalibrationYear + "e"] = gblClibrationData[s].CalibrationData[t]['P' + ss];
                        }
                        if (gblClibrationData[s].CalibrationData[t].Axis.includes("Velocity")) {
                            data[gblClibrationData[s].CalibrationYear + "v"] = gblClibrationData[s].CalibrationData[t]['P' + ss];
                        }
                    }
                    SrtData.push(data);
                }
            }
    

    still, will appreciate if any better way to do it...


  2. 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.

    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"}]}];
    
    const dictAxis = {
      "p": "Profile",
      "s": "Symmetry",
      "e": "Error (%)",
      "v": "Velocity (m/s)",
    };
    
    const countPoints = 10;
    
    function transform(arr) {
      var result = [];
      
      arr.forEach(function(itemYearData) {
        var year = itemYearData["CalibrationYear"]
        
        // normalize data of points to a
        // {"prop": [points...], "other": [points...]}
        var data = itemYearData["CalibrationData"].reduce(function(agg, item) {
          var points = [];
          for (var i = 1; i <= countPoints; i++) {
            var key = "P" + i;
            points.push(item[key])
          }
          agg[item["Axis"]] = points;
          return agg;
        }, {})
    
        // console.log(data)
    
        // building item of series for this year
        // and pushing it to result
        for (var i = 0; i < countPoints; i++) {
          var item = {}
          for (var letter in dictAxis) {
            var keyProp = dictAxis[letter];
            var keyYear = "" + year + letter;
            var value = data[keyProp][i]
            item[keyYear] = value;
          }
          result.push(item)
        }
    
      })
    
      return result;
    }
    
    var CalibrationData = transform(gblCalibrationData)
    console.log(CalibrationData)
    .as-console-wrapper {max-height: 100% !important}
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search