检查对象数组

checking array of objects

我想检查 inspectionScheduleUnitContactArtifactDto 对象数组的每个长度,如果有一个 inspectionScheduleUnitContactArtifactDto 长度等于 0 return true,如果 return 的每个长度=11=] 不为 0 或 inspectionScheduleUnitContactArtifactDto return 中没有 0 长度 false;

#样本对象

{
    "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": [],
        }
    ]
}

我假设您知道对象的布局,并且您有 类 包含所述数据。我创建了这些示例 类 来模拟您在此处显示的数据:

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[]) { }
}

您可以使用过滤器并检查结果数组的长度,如下所示:

// 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;

您可以使用递归函数,并传递您想要确定的任何键,以确定您的复杂对象中是否存在具有相同名称的空属性:

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'));

我相信您的问题可以简化为以下内容: 如果至少一个对象的 inspectionScheduleUnitContactArtifactDto 数组为空 -> return true 否则 return false。 最简单的方法是使用 some,只要满足此条件,它就会 return true 而不是过滤整个数组。如果遍历数组后不满足条件,则return false.

我使用了以下接口来更轻松地处理您的对象结构:

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
}

导入该接口,可以使用以下功能:

containsZeroLengthArray(inspectionSchedule: InspectionSchedule) {
  return inspectionSchedule.inspectionScheduleUnitArtifactDto
    .some(contact => !contact.inspectionScheduleUnitContactArtifactDto.length);
}

注意:!someArray.length 是空数组的 true