cat1244
cat1244

Reputation: 69

Concat Observable

I have a method that returns an array of objects

I need to filter it by a certain criterion, and then create a new array of objects, where the data will be converted to another model

and then return the resulting array, then subscribe to it

public getTestWithPare(): Observable<Array<TestModel>> {

    return this.http.getTests().pipe(
        flatMap(rate => rate), // splitting array
        filter(test => !test.isDone),
        map(test => {

            const testPare = new TestModel();
            testPare.key = `${test.id}/${test.name}`;
            if (test.type === 'new') {
                testPare.finish = test.value;
                testPare.show = true;
            } else {
                testPare.start = test.value;
                testPare.hide = true;
            }

            return testPare;
        }),
        concatAll(),
        toArray()
    );
}

after calling the method, I get an error:

You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

I can not understand what is wrong

I want to get an array of objects at the output

approximately in this format:['key': {key: '1 / t', finish: 7, show: true}, ...]

Upvotes: 0

Views: 176

Answers (1)

Maksim Romanenko
Maksim Romanenko

Reputation: 365

Rxjs is overkill here (only if you have big arrays and you don't want to handle it in 1 go), user array methods

const testToTestPare = test => {
  /* If you don't need TestMode, create an empty object const testPare = {} */
  const testPare = new TestModel(); 
  testPare.key = `${test.id}/${test.name}`;
  if (test.type === 'new') {
    testPare.finish = test.value;
    testPare.show = true;
  } else {
    testPare.start = test.value;
    testPare.hide = true;
  }
  return testPare;
};


public getTestWithPare(): Observable<Array<TestModel>> {
    return this.http.getTests().pipe(
        map(tests => tests.map(testToTestPare)),
    );
}

Upvotes: 1

Related Questions