0

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"
            }
        ]
    }
]
Abhinav
  • 1,202
  • 1
  • 8
  • 12

1 Answers1

0

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);
  • your need to replace line `validateObj[key] = innerArr.every((ele) => ele);` with `validateObj[key] = innerArr.findIndex(e => e === "") != -1 && innerArr.findIndex(e => e !== "") != -1;` – Sandeep Jun 14 '23 at 08:06
  • As per him, return true only If all the P1 from array object must be have value, so every is check that condition – Krunal Jethi Jun 14 '23 at 09:39
  • User asked " if P1 have value for any Axis then all other Axis should also have value for P1" otherwise not. So only P1 and P2 must have values in all objects. For all other keys values are blank in each object in array so no need to check that one – Sandeep Jun 14 '23 at 09:42
  • @KrunalJethi@Sandeep this works partially, so if P1 has value it returns "True" and others returns "False" where as I need if user enter all values in P1 and any in P2, P3, P4 etc but not complete, so complete loop should return "false" – Abhinav Jun 14 '23 at 10:36
  • @Abhinav do you mean if any one from P1 to P10 fails validation then all should be marked as failed? If possible include 2-3 expected outputs in question – Sandeep Jun 14 '23 at 13:15
  • I have updated code , please check @Abhinav – Krunal Jethi Jun 15 '23 at 05:28
  • @Sandeep yes correct – Abhinav Jun 15 '23 at 11:29