【问题标题】:Using Observable map operator to flatted an array使用 Observable map 运算符展平数组
【发布时间】:2017-06-24 15:54:56
【问题描述】:

假设我有一个函数,它返回下面对象ObjectReturnedObservable

interface ObjectReturned {
  id: string,
    information: info[],
    anotherObj: AnotherObj[]
}

interface AnotherObj {
  information: info[]
}

interface info {
  name: string
}

鉴于ObjectReturned.info[]AnotherObj.info[]每个都只包含一个元素,我如何使用Observablemap 运算符来“展平”上面的info 数组这样我就可以直接使用objectReturned.nameobjectReturned.anotherObj.name 访问返回的结果(Observable<ObjectReturned>)?

【问题讨论】:

  • observable 与此无关。只需使用地图,然后自己将其展平。 const [firstAndOnlyValue] = yourArray;然后根据需要使用 firstAndOnlyValue
  • 可能我没说清楚,抱歉,我还是想返回一个 ObjectReturned 的 observable

标签: rxjs observable rxjs5


【解决方案1】:
objectReturned.map( obj => Object.assign(obj, {name: information[0].name});

这给了

{
  id: string,
  name: string,
    anotherObj: AnotherObj[]
}

您也可以进一步将其应用于anotherObj。让我知道这是否是您正在寻找的。

【讨论】:

    猜你喜欢
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    • 2019-09-11
    • 2017-03-17
    • 2019-09-04
    相关资源
    最近更新 更多