gpt4 book ai didi

rxjs - 使用flatMap将数组转换为项目序列

转载 作者:行者123 更新时间:2023-12-04 07:32:53 25 4
gpt4 key购买 nike

在RxJS中,我想将某个点上的数组转换为数组中的一系列项目。我找到了两种方法来做:我猜选项1和2做同样的事情:

const obj = { array: [1, 2, 3, 4, 5] };

const observable = Observable.of(obj);

// Option 1
observable.flatMap(x => {
return Observable.from(x.array);
}).subscribe(console.log);

// Option 2
observable.flatMap(x => x.array).subscribe(console.log);

// Option 3 ?

有没有更好/更好的方式来表达我在做什么,我的意思是没有 flatMap运算符?

最佳答案

我认为您已经达到了最短的可能。我可能建议的唯一改进是完全避免使用回调函数:

const obj = { array: [1, 2, 3, 4, 5] };
const observable = Observable.of(obj);

observable
.pluck('array')
.concatAll() // or mergeAll()
.subscribe(console.log);

观看现场演示: https://jsbin.com/zosonil/edit?js,console

关于rxjs - 使用flatMap将数组转换为项目序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44861782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com