我正在尝试对两个数组进行补充,并用剩余的数据填充第三个数组。我有一个this.subjects
对象和一个this.teacerData
属性,从中我取出了主题代码并将它们放在两个不同的数组中。现在,我试图过滤掉两个数组中不相等的数据,并将其放入第三个数组中。
subj: Subjects[];
teacherSubjects: string[];
data: string[];
filteredData: string[];
addsubject: AddTeacherSubject = new AddTeacherSubject();
teacherData: TeacherUpdateDataDto = new TeacherUpdateDataDto();
ngOnInit() {
this._curriculumService.getSubject(this.appSession.tenant.tenancyName)
.finally(() => { this.saving = false;})
.subscribe((result: listResultDtoOfSubjectDto) => {
this.subjects = result.items;
this.teacherSubjects = this.subjects.map(a => a.code);
console.log("All Subjects =" + this.teacherSubjects);
})
this._teacherService.GetTeacherUpdateData(this.appSession.tenant.tenancyName,
this._sharedService.getMessage())
.finally(()=> { this.saving = false;})
.subscribe((result: TeacherUpdateDataDto) => {
this.teacherData = result;
this.subj = this.teacherData.subject;
this.data = this.subj.map(a => a.code);
})
}
this.subjects DTO
(3) [subjectlistDto, subjectlistDto, subjectlistDto]
0 :
subjectlistDto {name: "Mathematics", code: "mathematics", classCode: "bscs",
classNAME: "BSCS"}
1 :
subjectlistDto {name: "English", code: "english", classCode: "bscs",
classNAME: "BSCS"}
2 :
subjectlistDto {name: "Islamiat", code: "islamiat", classCode: "bscs",
classNAME: "BSCS"}
this.teacherData Dto
subject:Array(2)
0:
{code: "mathematics"}
1:
{code: "english"}
我需要filteredSubjects数组中的已过滤数据。我该怎么办?
答案 0 :(得分:0)
这取决于您使用的rxjs版本,但请查看forkJoin
或combineLatest
。这些使您可以将多个可观察值的结果合并为一个请求。
Combine latest example和Fork join。发射完两组数据后,比较要过滤的项目就很简单了。
编辑:如果只想过滤一次出现的项目的数组,则执行以下操作:
function getDistinctValues(array1, array2): [] {
const finalArray = [...array1, ...array2];
finalArray = finalArray.sort((item1, item2) => {
return item1 === item2 ? 0 : item1 > item2 ? 1 : -1;
});
let pointer = finalArray.length-1;
while(pointer > 0) {
if(finalArray[pointer] === finalArray[pointer-1]) {
finalArray.splice(pointer-1, 2);
pointer--;
}
pointer--;
}
return finalArray;
}
答案 1 :(得分:0)
这是我的做法
只是从ngOnInit和
调用了数据功能Data(subjec: string[], compar: string[]) {
this.filteredData = [];
for(var i = 0; i< subjec.length; i++) {
if(compar.indexOf(subjec[i]) === -1){
this.filteredData.push(subjec[i]);
}
}
for(var j = 0; j<compar.length; j++) {
if(subjec.indexOf(compar[j]) === -1){
this.filteredData.push(compar[j]);
}
}
return this.filteredData;
}