Nested Object Loop

IF you want to find the value for some specific key which is a nested object, like shown below, if we want to get “custcol_ag_bankname” object value from this we can use the code shown below

                "data": {
                    "bankInfo": {
                        "custcol_ag_bankname": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_bankaddress": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_bankaddress2": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_bankfraction": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_routingnumber": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_checkaccountnumber": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_startingnumber": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_reversenumbering": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_bankpersonlizationcolor": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_bankpersonlizationfont": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" }
                    },
                    "companyInfo": {
                        "custcol_ag_companyname1": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_companyname2": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_companyname3": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_address1": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_address2": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_companypersonlizationfont": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_companypersonlizationcolor": {
                            "displayOption": true,
                            value: "",
                            refValue: "",
                            oldvalue: "",
                            temp: "",
                            marksame: ["custcol_ag_bankpersonlizationcolor"]
                        },
                        "custcol_ag_extralineaddress": { "displayOption": false, value: "", refValue: "", oldvalue: "", temp: "" }
                    },
                    "logoInfo": {
                        "custcol_ag_logolayout": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_logooptionselectbox": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_logocolors": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "confirmCheckbox": { "displayOption": false, value: "", refValue: "", oldvalue: "", temp: "" },
                        "uploadLogo": {
                            "displayOption": false, value: "", refValue: "", oldvalue: "", temp: "",
                            dependency: [
                                "custcol_ag_logolayout",
                                "custcol_ag_logooptionselectbox",
                                "custcol_ag_logocolors",
                                "confirmCheckbox"
                            ]
                        }
                    },
                    "additionalInfo": {
                        "custcol_ag_ezshield": {
                            "displayOption": true,
                            value: "",
                            refValue: "",
                            oldvalue: "", temp: "",
                            data: { true: { "custcol_ag_ezshield_item": 1253 }, false: { "custcol_ag_ezshield_item": "" } }
                        },
                        "custcol_ag_signaturelines": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_titleabovesign": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_secondarytitleabove": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_imprint_below_sign": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_textonstub": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_logopersonlizationcolor": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" },
                        "custcol_ag_logopersonlizationfont": { "displayOption": true, value: "", refValue: "", oldvalue: "", temp: "" }
                    },
                    "hiddenOptions": {}
                }
const keyToFind = "custcol_ag_bankname";
const sectionKeys = _.keys(temp.data);

_.some(sectionKeys, function(sectionKey) {
  if (_.has(temp.data[sectionKey], keyToFind)) {
    const targetObject = temp.data[sectionKey][keyToFind];
    console.log(targetObject);
    return true; // If you want to find only the first occurrence
  }
});

Leave a comment

Your email address will not be published. Required fields are marked *