I have an object that has data (null) and other objects with an array whose values are also null, if all values are null then false, if at least one is something else then true. How do I parse this object and see if all the data is null and get true/false
Object { employerCompanyName: null, companyBenefitsProviderName: null, companyRepresentative: null, website: null, logonID: null, password: null, benefitsDetails: (1) […], coverageDetails: (1) […], beneficiaryDetails: (1) […], notes: null }
beneficiaryDetails: Array [ {…} ] 0: Object { beneficiary: null, allocation: null } length: 1 : Array [] benefitsDetails: Array [ {…} ] 0: Object { benefitsPackageType: null, benefitsCoverageDetails: [], policyNumber: null } length: 1 : Array [] companyBenefitsProviderName: null companyRepresentative: null coverageDetails: Array [ {…} ] 0: Object { familyMemberCovered: null, currency: null, amount: null } length: 1 : Array [] employerCompanyName: null logonID: null notes: null password: null website: null
Created the test objects to spec. Defined logic for setting flag and logging values when something other than null exists either inside object or within an array in that object.
Pass object to Object.entries for testing. Simply wrap this logic in a function that passes the parameter along into Object.entries and return out whatever is required.
let thing1 = {a: null, b: [null, null, null]};
let thing2 = {a: null, b: [null, null, 'value']};
let thing3 = {a: 'something', b:[null, null, null]};
let flag = false;
for( [key,val] of Object.entries(thing1) ){
if(val && !Array.isArray(val)){
console.log(val);
flag = true;
} else if(Array.isArray(val)){
for(let i = 0; i < val.length; i++){
if(val[i] !== null){
console.log( val[i] );
flag = true;
}
}
}
}
Remains false for thing1, flag set to true and logs value for thing2 and thing3.