두 클래스 모델을 비교하고 해당 차이점을 찾으려면 어떻게합니까? 다음은 정확히 동일한 멤버를 가진 두 모델이며 비교해야합니다.
이것을 수행하는 Typescript에 알고리즘이 있습니까? * 새로운 결과 클래스는 클래스 멤버와 부울 다른 플래그로 생성되어야합니까?
Typescript 알고리즘에서 쉬운 방법을 찾고 있습니다. 모든 수업을 수락해야합니다.
참고 : 클래스의 일부 멤버는 클래스 자체를 포함합니다.
현재
수업:
export class PropertyLocation {
streetName: string;
streetType: string;
postdirectional?: string;
unitNumber?: string;
unitType?: string;
city: string;
state?: string;
postalCode: number;
postalCodeExtension?: string;
effectiveStartDate: Date;
addressChangeReason?: AddressChangeReasonDto
addressSource?: SourceOfAddressDto;
}
결과 클래스 배열 샘플 :
더 최적의 저장 방법이 있다면 자유롭게 수정하십시오
export class DifferenceClass {
ClassMember: string;
DifferentFlag: boolean
}
코드 솔루션을 찾고, 회사가 선호하지 않는 타사 라이브러리 사용을 자제
비교를 실행하는 클래스에 메서드 / 함수를 빌드하지 않는 이유는 무엇입니까? 각 속성을 개별적으로 비교 (모든 것을 입력)하거나 클래스의 모든 키를 반복하고 전달 된 객체의 모든 키와 비교하여이를 수행 할 수 있습니다.
class DifferenceClass {
constructor(classMember, differenceFlag) {
this.ClassMember = classMember;
this.DifferenceFlag = differenceFlag;
}
}
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
isSame(person) {
if (person instanceof Person) {
const differences = [];
for (const key in person) {
if (this[key] !== person[key]) {
differences.push(new DifferenceClass(key, true));
}
}
return differences;
} else {
throw new Error('Object is not a Person class');
}
}
}
const p1 = new Person('John', 'Doe');
const p2 = new Person('Jane', 'Doe');
console.log('Should be empty', p1.isSame(p1));
console.log('Should have diff results', p1.isSame(p2));
console.log('should be an exception', p1.isSame(1));
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다