Home > Net >  checking empty property for array of objects
checking empty property for array of objects

Time:12-15

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

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
    }
]

CodePudding user response:

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 === "")))

  • Related