简体   繁体   中英

How to check if a class element is set?

is there a way to catch all fields that have not been set in a class ? i am parsing a json file and sometimes field names change and i would like to know which fields have not been set to make it simpler to fix. Below is a sample of my code i use to sett the fields in class. This is only small sample , my class has over 250 fields so checking one by one is not going to work.

const myLand = new mls.Land();
    myLand.landLease = trimString(data["LAND LEASE?"]);
    myLand.commonInterest = trimString(data["COMMON INTEREST"]);
    myLand.landLeaseAmount = trimString(data["LAND LEASE AMOUNT"]);
    myLand.landLeaseAmtFreq = trimString(data["LAND LEASE AMT FREQ"]);
    myLand.landLeasePurch = trimString(data["LAND LEASE PURCH?"]);
    myLand.landLeaseRenew = trimString(data["LAND LEASE RENEW"]);

newListing.land = myLand;

and here is the Trim function

function trimString(inputStr: string) {
    return (inputStr !== undefined && typeof inputStr === "string") ? inputStr.trim() : undefined;
  }

Using Object.entries() , filter by value and map by key .

 const objWithUndefinedValues = { a: undefined, b: 2, c: "3" } const undefinedKeys = Object.entries(objWithUndefinedValues) .filter(([, value]) => value === undefined) .map(([key, ]) => key) console.log(undefinedKeys); 

If you need to check whose keys of the object representing your class have been set to undefined , you can use Object.keys() and Array.filter() like this:

 function trimString(inputStr) { return (inputStr !== undefined && typeof inputStr === "string") ? inputStr.trim() : undefined; } const myLand = {}; myLand.landLease = trimString("123"); myLand.commonInterest = trimString(" common interest "); myLand.landLeaseAmount = trimString(null); myLand.landLeaseAmtFreq = trimString({}); let undefinedKeys = Object.keys(myLand).filter(k => myLand[k] === undefined); console.log("Undefined fields:", undefinedKeys); 
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;} 

You could just create an array of expected fields, and then filter those:

const expectedFields = ["LAND LEASE?", "COMMON INTEREST", "LAND LEASE AMOUNT", "LAND LEASE AMT FREQ", "LAND LEASE PURCH?", "LAND LEASE RENEW"];

const absentFields = expectedFields.filter(field => !(field in data));

If you want to also capture fields which are not of type string:

const absentFields = expectedFields.filter(field => typeof data[field] !== "string");

Your routine for assigning the data, could also be put in a loop:

 const camelCase = field => field.match(/\\w+/g).map((word, i) => (i ? word[0] : "") + word.slice(+!!i).toLowerCase()).join``; const expectedFields = ["LAND LEASE?", "COMMON INTEREST", "LAND LEASE AMOUNT", "LAND LEASE AMT FREQ", "LAND LEASE PURCH?", "LAND LEASE RENEW"]; // Sample data const data = { "LAND LEASE?": "a", "LAND LEASE AMOUNT": "n" } const myLand = {}; for (const field of expectedFields) { const value = data[field]; if (typeof value === "string") myLand[camelCase(field)] = value.trim(); } console.log(myLand); console.log("missing:", expectedFields.filter(field => typeof data[field] !== "string")); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM