Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

checking empty property for array of objects

I wanted to check if there is a property in an array of objects that has a null value , if there is then return true.

Currently my issue is

This condition will always return 'false' since the types '{ type: any; startDate: string; endDate: string; annualRent: any; }' and 'string' have no overlap.ts(2367)

#current code for checking empty property

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const hasEmptyRentSchedProperty = Object.values((this.proposedRentSchedule)).some((v => v === null || v === ""))

#object = this.proposedRentSchedule

[
    {
        "type": 0,
        "startDate": "2022-1-4",
        "endDate": "2022-1-19",
        "annualRent": "232"
    },
    {
        "type": 0,
        "startDate": "2022-1-20",
        "endDate": "2022-1-20",
        "annualRent": null
    }
]

>Solution :

The reason you get that error is because you are getting the values of array whose type is { type: any; startDate: string; endDate: string; annualRent: any; } and not just string. In this case, how does a string comparison work? That’s what Typescript points out.

var data = [
  {
    "type": 0,
    "startDate": "2022-1-4",
    "endDate": "2022-1-19",
    "annualRent": "232"
  },
  {
    "type": 0,
    "startDate": "2022-1-20",
    "endDate": "2022-1-20",
    "annualRent": null
  }
]

console.log(data.some(element => Object.values(element).some(val => val === null || val === "")))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading