【问题标题】:Concat Observable连续可观察
【发布时间】:2018-12-18 16:15:21
【问题描述】:

我有一个返回对象数组的方法

我需要按照一定的标准对其进行过滤,然后创建一个新的对象数组,其中数据将转换为另一个模型

然后返回结果数组,然后订阅它

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()
    );
}

调用方法后报错:

您在预期流的位置提供了无效对象。你可以 提供 Observable、Promise、Array 或 Iterable。

我不明白出了什么问题

我想在输出端得到一个对象数组

大概是这样的格式:['key': {key: '1 / t', finish: 7, show: true}, ...]

【问题讨论】:

  • 我看了,但这不是我的情况,我想将 Observable 集成到一个数组中,也许我做得不对
  • 简单地删除concatAll怎么样? toArray 应该足够了,如果我理解正确你的情况
  • 它将返回一个模型数组 [TestModel, TestModel, ...] 并且使用它们不是很方便。
  • 因为我想返回格式为 ['key': {key: '1 / t', finish: 7, show: true}, ...] 的数组。这样我就可以通过键值从这个对象中获取 html

标签: typescript rxjs rxjs6


【解决方案1】:

Rxjs 在这里是多余的(只有当你有大数组并且你不想一次性处理它时),用户数组方法

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)),
    );
}

【讨论】:

    猜你喜欢
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多