rxjs - 如何合并两个观察的结果(不同类型)
问题描述
假设我有这两个观察结果:
const thread = of({
thread: {
name: "Name",
author: null
}
})
const author = of({name:"Snoob"})
我怎样才能得到这些观察的合并结果:
const threadWithAuthor = .....;
threadWithAuthor.subscribe(it=>console.log(it))
// {
// thread: {
// name: "Name",
// author: { name: "Snoob" }
// }
// }
解决方案
这是一个如何使用combineLatest
、pipe
和的示例map
:
var {of, combineLatest } = require('rxjs')
var { map } = require('rxjs/operators')
var mergeByAuthor = ([t, a]) => {
var x = Object.assign({}, t)
x.thread.author = a
return x
}
var thread = of({
thread: {
name: 'Name',
author: null
}
})
var author = of({name:'Snoob'})
var threadWithAuthor = combineLatest(thread, author).pipe(
map(mergeByAuthor)
)
threadWithAuthor.subscribe(x => console.log(JSON.stringify(x, null, 2)))
输出
{
"thread": {
"name": "Name",
"author": {
"name": "Snoob"
}
}
}
推荐阅读
- scala - json4s 抛出 NoClassDefFoundError: java/sql/Timestamp
- mql4 - 无论单击“是”还是“否”按钮,消息框都会关闭交易
- javascript - 从 generateDataKeyPairWithoutPlaintext() 获取字符串数据
- python - 关于 Python 简写的问题
- azure - 将映像从 Azure 容器注册表拉取到 Kubernetes 集群
- python - Python sort_insertion 函数无法正常工作
- php - 发送POST时php获取名称输入文件
- python - 使用olefile方法从目录中打开excel文件
- java - 如何打印存储在字符串中的几个值?
- sql-server - 带有问题 ole db 命令的 SSIS 更新 SQL 任务