Issue
I wanted to check if each length of array of objects of inspectionScheduleUnitContactArtifactDto
, if there is one inspectionScheduleUnitContactArtifactDto
which length is equal to 0 return true , if each length of inspectionScheduleUnitContactArtifactDto
not 0 or there is no 0 length from inspectionScheduleUnitContactArtifactDto
return false;
#sample object
{
"id": 218,
"propertyId": 22977,
"inspectionScheduleUnitArtifactDto": [
{
"id": 524,
"inspectionScheduleUnitContactArtifactDto": [
{
"id": 1097,
"inspectionScheduleUnitArtifactId": 524,
"primaryContactName": "fsdfsdf",
}
],
},
{
"id": 525,
"inspectionScheduleUnitContactArtifactDto": [
{
"id": 1100,
"inspectionScheduleUnitArtifactId": 525,
"primaryContactName": "Name",
},
{
"id": 1101,
"inspectionScheduleUnitArtifactId": 525,
"primaryContactName": "342423432",
}
],
},
{
"inspectionScheduleUnitContactArtifactDto": [],
}
]
}
Solution
I believe your question can be simplified to the following:
If at least one object's inspectionScheduleUnitContactArtifactDto
array is empty -> return true else return false.
The simplest way to do this is with some
which will return true
as soon as this condition is met rather than filtering through the entire array. If the condition is not met after searching through the array, then it will return false
.
I used the following interfaces to make working with your object structure a bit easier:
export interface InspectionSchedule {
id: number,
propertyId: number,
inspectionScheduleUnitArtifactDto: InspectionScheduleUnitArtifactDto[]
}
export interface InspectionScheduleUnitArtifactDto {
id: number,
inspectionScheduleUnitContactArtifactDto: InspectionScheduleUnitContactArtifactDto[]
}
export interface InspectionScheduleUnitContactArtifactDto {
id: number,
inspectionScheduleUnitArtifactId: number,
primaryContactName: string
}
Importing this interface, you can use the following function:
containsZeroLengthArray(inspectionSchedule: InspectionSchedule) {
return inspectionSchedule.inspectionScheduleUnitArtifactDto
.some(contact => !contact.inspectionScheduleUnitContactArtifactDto.length);
}
Note: !someArray.length
is true
for an empty array.
Answered By - Joshybull
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.