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": [],
}
]
}
CodePudding user response:
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.
CodePudding user response:
I'm going to assume that you know the layout of your object and that you have classes that contain said data. I've created these sample classes to mock the data you showed here:
export class A {
constructor(id: number,
inspectionScheduleUnitArtifactId: number,
primaryContactName: string) { }
}
export class B {
constructor(id: number | null,
inspectionScheduleUnitContactArtifactDto: A[]) { }
}
export class C {
constructor(id: number, propertyId: number,
inspectionScheduleUnitArtifactDto: B[]) { }
}
You can use filter and check the length of the resulting array, like so:
// Setup mock data
const a1: A = new A(1097, 524, 'fsdfsdf');
const a2: A = new A(1100, 525, 'Name');
const a3: A = new A(1101, 525, '342423432');
const b1: B = new B(524, [a1]);
const b2: B = new B(525, [a2, a3]);
const b3: B = new B(null, []);
const c: C = new C(218, 22977, [b1, b2, b3]);
// Check if any of the inner inspectionScheduleUnitContactArtifactDtos are empty
const anyEmpty: boolean = c.inspectionScheduleUnitArtifactDto
.filter(x => x.inspectionScheduleUnitContactArtifactDto.length === 0)
.length > 0;
CodePudding user response:
You can use a recursive function, and pass whatever key you want to determine if an empty property that has the same name exists in your complex objects:
var obj = {
"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": [],
}
]
}
function getEmptyProp(obj, key) {
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if ( obj[i].constructor === Array || obj[i].constructor === Object ) {
if (i == key) {
if (obj[i].length) {
if (getEmptyProp(obj[i], key)) return true;
} else {
return true;
}
} else {
if (getEmptyProp(obj[i], key)) return true;
}
} else {
if (i == key) return true;
}
}
return false;
}
// will return 'true' in this case
console.log(getEmptyProp(obj, 'inspectionScheduleUnitContactArtifactDto'));