简体   繁体   English

如何避免嵌套循环

[英]How to avoid nested loops

I need to construct this object wit the has and repo. 我需要用has和repo构造这个对象。 I feel like nesting these maps is a really bad practice, and it's the point of failure for the flow. 我觉得嵌套这些地图是非常糟糕的做法,这是流程失败的地方。 How should this be written? 应该怎么写?

let observ = observable$.flatMap(repos => {
  return Rx.Observable.from(repos.map(repo => ({hash: getHash(repo), repo})))
})

I don't see anything explicitly wrong with this approach. 我没有发现这种方法有任何明显的错误。 If you would like to simplify the code, you can use concatMap , which will transform a stream of arrays to a stream of the items. 如果您想简化代码,可以使用concatMap ,它将数组的流转换为项目的流。 We can then just do a normal map: 然后我们可以做一个法线贴图:

let repo$ = observable$.concatMap(identity);
let observ = repo$.map(repo => ({hash: getHash(repo), repo }));

Where identity === x => x . identity === x => x

You can also apply concatMap earlier in your chain when constructing the observable$ stream if you want to avoid calling it with identity . 如果要避免使用identity调用它,也可以在构造observable$流时在链中的较早位置应用concatMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM