No Null Column array

58 views Asked by At

I've an array like below

[
    {
        "Axis": "Profile",
        "P1": 12,
        "P2": "",
        "P3": "",
        "P4": "",
        "P5": "",
        "P6": "",
        "P7": "",
        "P8": "",
        "P9": "",
        "P10": "",
        "year": ""
    },
    {
        "Axis": "Symmetry",
        "P1": 23,
        "P2": "",
        "P3": "",
        "P4": "",
        "P5": "",
        "P6": "",
        "P7": "",
        "P8": "",
        "P9": "",
        "P10": "",
        "year": ""
    },
    {
        "Axis": "Error (%)",
        "P1": "",
        "P2": 1,
        "P3": "",
        "P4": "",
        "P5": "",
        "P6": "",
        "P7": "",
        "P8": "",
        "P9": "",
        "P10": "",
        "year": ""
    },
    {
        "Axis": "Velocity (m/s)",
        "P1": 22,
        "P2": "",
        "P3": "",
        "P4": "",
        "P5": "",
        "P6": "",
        "P7": "",
        "P8": "",
        "P9": "",
        "P10": "",
        "year": ""
    }
]

what I want is, if P1 have value for any Axis then all other Axis should also have value for P1 and similar validation for rest of key.. if user miss to add any value, it should alert error..

any suggestion apart from loopp.. I tried below logic, its not working ...

var isProfileDataVal = false;
    var isSymmetryVal = false;
    var isErrorVal = false;
    var isVelocityVal = false;

    for (var i = 0; i < gbl_ClibrationData.length; i++) {

        for (var j = 0; j < 4; j++) {
            if (Caldata.CalibrationData[i].CalibrationData[j].P1 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
                
            }
            if (Caldata.CalibrationData[i].CalibrationData[j].P2 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
            }
            if (Caldata.CalibrationData[i].CalibrationData[j].P3 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
            }
            if (Caldata.CalibrationData[i].CalibrationData[j].P4 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
            }
            if (Caldata.CalibrationData[i].CalibrationData[j].P5 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
            }
            if (Caldata.CalibrationData[i].CalibrationData[j].P6 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
            }
            if (Caldata.CalibrationData[i].CalibrationData[j].P7 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
            }
            if (Caldata.CalibrationData[i].CalibrationData[j].P8 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
            }
            if (Caldata.CalibrationData[i].CalibrationData[j].P9 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
            }
            if (Caldata.CalibrationData[i].CalibrationData[j].P10 != "") {
                if (j === 0)
                    isProfileDataVal = true;
                if (j === 1)
                    isSymmetryVal = true;
                if (j === 2)
                    isErrorVal = true;
                if (j === 3)
                    isVelocityVal = true;
            }
        }
    }
    if (!isProfileDataVal || !isSymmetryVal || !isErrorVal || !isVelocityVal) {
        swal("Failed", "Please add calibration period data for Profile,Symmetry,Error,Velocity!", "error");
        return;
    }

below is the object array for loop

[
    {
        "ChartId": "1",
        "CalibrationData": [
            {
                "CalibrationYear": "2012",
                "CalibrationData": [
                    {
                        "Axis": "Profile",
                        "P1": 12,
                        "P2": "",
                        "P3": "",
                        "P4": "",
                        "P5": "",
                        "P6": "",
                        "P7": "",
                        "P8": "",
                        "P9": "",
                        "P10": "",
                        "year": ""
                    },
                    {
                        "Axis": "Symmetry",
                        "P1": 23,
                        "P2": "",
                        "P3": "",
                        "P4": "",
                        "P5": "",
                        "P6": "",
                        "P7": "",
                        "P8": "",
                        "P9": "",
                        "P10": "",
                        "year": ""
                    },
                    {
                        "Axis": "Error (%)",
                        "P1": "",
                        "P2": 1,
                        "P3": "",
                        "P4": "",
                        "P5": "",
                        "P6": "",
                        "P7": "",
                        "P8": "",
                        "P9": "",
                        "P10": "",
                        "year": ""
                    },
                    {
                        "Axis": "Velocity (m/s)",
                        "P1": 22,
                        "P2": "",
                        "P3": "",
                        "P4": "",
                        "P5": "",
                        "P6": "",
                        "P7": "",
                        "P8": "",
                        "P9": "",
                        "P10": "",
                        "year": ""
                    }
                ],
                "CalibrationMeterName": "FT35101A",
                "CalibrationTags": [
                    "Blocks_127.ProfileFactor",
                    "Blocks_022.AvgFlow"
                ],
                "CalibrationApiName": "USM"
            },
            {
                "CalibrationYear": "3022",
                "CalibrationData": [
                    {
                        "Axis": "Profile",
                        "P1": 1,
                        "P2": "",
                        "P3": "",
                        "P4": "",
                        "P5": "",
                        "P6": "",
                        "P7": "",
                        "P8": "",
                        "P9": "",
                        "P10": "",
                        "year": ""
                    },
                    {
                        "Axis": "Symmetry",
                        "P1": "",
                        "P2": 1,
                        "P3": "",
                        "P4": "",
                        "P5": "",
                        "P6": "",
                        "P7": "",
                        "P8": "",
                        "P9": "",
                        "P10": "",
                        "year": ""
                    },
                    {
                        "Axis": "Error (%)",
                        "P1": "",
                        "P2": "",
                        "P3": 1,
                        "P4": "",
                        "P5": "",
                        "P6": "",
                        "P7": "",
                        "P8": "",
                        "P9": "",
                        "P10": "",
                        "year": ""
                    },
                    {
                        "Axis": "Velocity (m/s)",
                        "P1": "",
                        "P2": "",
                        "P3": "",
                        "P4": 1,
                        "P5": "",
                        "P6": "",
                        "P7": "",
                        "P8": "",
                        "P9": "",
                        "P10": "",
                        "year": ""
                    }
                ],
                "CalibrationMeterName": "FT35101A",
                "CalibrationTags": [
                    "Blocks_127.ProfileFactor",
                    "Blocks_022.AvgFlow"
                ],
                "CalibrationApiName": "USM"
            }
        ]
    }
]
1

There are 1 answers

6
Krunal Jethi On

Below is the logic which you can use, Let me know if you have any query

 let a = [
  {
    Axis: "Profile",
    P1: 12,
    P2: "",
    P3: "",
    P4: "",
    P5: "",
    P6: "",
    P7: "",
    P8: "",
    P9: "",
    P10: "",
    year: "",
  },
  {
    Axis: "Symmetry",
    P1: 23,
    P2: "",
    P3: "",
    P4: "",
    P5: "",
    P6: "",
    P7: "",
    P8: "",
    P9: "",
    P10: "",
    year: "",
  },
  {
    Axis: "Error (%)",
    P1: "",
    P2: 1,
    P3: "",
    P4: "",
    P5: "",
    P6: "",
    P7: "",
    P8: "",
    P9: "",
    P10: "",
    year: "",
  },
  {
    Axis: "Velocity (m/s)",
    P1: 22,
    P2: "",
    P3: "",
    P4: "",
    P5: "",
    P6: "",
    P7: "",
    P8: "",
    P9: "",
    P10: "",
    year: "",
  },
];
let validateObj = {};
Object.keys(a[0]).forEach((key) => {
  let innerArr = a.map((ele) => ele[key]);
  console.log(key,!!a[0][key]);
  validateObj[key] = innerArr.every((ele) => !!ele === !!a[0][key]);
});
console.log(validateObj);