Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
comprobación de matriz de objetos

Quería verificar si cada longitud de la matriz de objetos de la inspectionScheduleUnitContactArtifactDto , si hay una inspectionScheduleUnitContactArtifactDto cuya longitud es igual a 0 devuelve verdadero, si cada longitud de la inspectionScheduleUnitContactArtifactDto no es 0 o no hay una longitud 0 de la inspectionScheduleUnitContactArtifactDto devuelve falso;

#objeto de muestra

 { "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": [], } ] }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Creo que su pregunta se puede simplificar a lo siguiente: si al menos la matriz de inspectionScheduleUnitContactArtifactDto de un objeto está vacía -> devuelve verdadero; de lo contrario, devuelve falso. La forma más sencilla de hacer esto es con some que devolverán true tan pronto como se cumpla esta condición en lugar de filtrar a través de toda la matriz. Si la condición no se cumple después de buscar en la matriz, devolverá false .

Utilicé las siguientes interfaces para facilitar un poco el trabajo con la estructura de su objeto:

 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 }

Al importar esta interfaz, puede utilizar la siguiente función:

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

Nota: !someArray.length es true para una matriz vacía.

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar una función recursiva y pasar cualquier clave que desee para determinar si existe una propiedad vacía que tiene el mismo nombre en sus objetos complejos:

 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'));
about 4 years ago · Juan Pablo Isaza Report

0

Asumiré que conoce el diseño de su objeto y que tiene clases que contienen dichos datos. Creé estas clases de muestra para simular los datos que mostraste aquí:

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

Puede usar el filtro y verificar la longitud de la matriz resultante, así:

 // 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;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!