2
votes

I have a BehaviourSubject history$ of type EvaluateHistoryItem[]. In uploadFile method I am splitting a uploaded .txt file by commas and then push them to resultSet[]. I want to append the values from resultSet[] to history$ BehaviorSubject.

I am new to RxJS, and find it complicated (still working on it). I would appreciate some help.


I tried something like this but it is not correct as I dont want to map, but append new values (I think it should be something with 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);

ps. Sorry if the title of the question is not good.


Here is my code:

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);
}  
1
resultSet.splice(0, 4) will remove the first four elements. - Devid

1 Answers

1
votes

The keywords which i read in your question are skip and bufferCount.

resultSet
  .skip(4) /* first four emissions */
  .bufferCount(4)
  .map(buffer => ({
    save: buffer[0],
    evaluate: buffer[1],
    result: buffer[2], 
    runTimeMs: buffer[3] 
  }))
  .subscribe(console.log);