我有一个 EvaluateHistoryItem[] 类型的 BehaviourSubject history$。在uploadFile 方法中,我用逗号分割上传的.txt 文件,然后将它们推送到resultSet[]。我想将 resultSet[] 中的值附加到 history$ BehaviorSubject。
我是 RxJS 的新手,发现它很复杂(仍在努力)。我会很感激一些帮助。
我尝试了这样的事情,但它不正确,因为我不想映射,而是附加新值(我认为它应该是 combineLatest 的东西):
history.map(a => {
a.saved = this.convertToBool(resultSet[0]);
a.evaluate = resultSet[1];
a.result = resultSet[2];
a.runTimeMs = Number(resultSet[3]);
a.tags.map(tag => tag.value = resultSet[4]);
});
this.history$.next(history);
附言。对不起,如果问题的标题不好。
这是我的代码:
interface EvaluateHistoryItem {
evaluate: string;
error?: string;
result?: string;
runTimeMs?: number;
saved?: boolean;
}
export class EvaluateComponent implements OnInit, AfterViewInit {
... ... ...
... ... ...
public history$ = new BehaviorSubject<EvaluateHistoryItem[]>([]);
... ... ...
public uploadFile(element: any) {
let uploadedFile = document.getElementById('uploadedFile');
let files: File[] = element.srcElement.files;
let file: File = files[0];
let reader = new FileReader();
let resultSet: string[] = [];
reader.onloadend = (result) => {
// replace new lines with commas and then split upon commas but not the ones inside quotes
let columns: string[] = reader.result.replace(/\n/g, ',').split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
for (let i = 0; i < columns.length - 1; i++) {
resultSet.push(columns[i]);
}
};
let history: EvaluateHistoryItem[] = this.history$.getValue();
// ... here I need to map values from resultSet
// ....
this.history$.next(history);
}