基于另一个数组向 Observable 数组添加额外的 属性
Add additional property to Observable array based on another array
我有两个 Observable<MyType>
类型的 Observable 数组
export interface MyType{
title: string;
id: string;
other: [];
}
我想向第一个数组添加额外的 属性 exists
并将其设置为 true
如果该项目存在于第二个数组中:
const combined$ = combineLatest(this.first$, this.second$);
this.output$ = combined.pipe(
map((x, y) => {
return x.map(a => {
a.title = a.title;
a.id = a.id;
a.other = a.other;
a.exists = y.find(b => b.id === a.id )
});
})
);
如果订阅 output
observable this.output$.subscribe(console.log);
,总是会得到 [...undefined]
结果
有什么解决办法吗?
我认为组合发送一个值,它是一组单独的值。这里 y
将是未定义的。
使用 ([x,y]) 解构 map 中的值,然后重试。
combined$ 也有一个你漏掉的拼写错误。
并且 find
可以替换为 some
以更好地表示逻辑和 return 布尔值
此外,当您使用 x.map
时,您在逻辑上映射了错误的数组。
const combined$ = combineLatest(this.first$, this.second$);
this.output$ = combined$.pipe(
map(([x, y]) => {
return x.map(a => {
a.title = a.title;
a.id = a.id;
a.other = a.other;
a.exists = y.some(b => b.id === a.id )
});
})
);
在你的代码片段中,你有一个拼写错误,你将 combinedLatest rxjs 运算符的结果设置为 combined$
,然后你在下一行将其称为 combined
,我认为这是不正确的,或者只是将此问题转换为 SO 时的翻译错误。 (不管怎样,一定要指出来嘿嘿)
接下来,combineLatest
运算符 returns 所有可观察值的数组。因此,您可以在 map
运算符中使用解构轻松地从所有可观察对象中获取最新值。
下面是最终代码:
const combined$ = combineLatest(this.first$, this.second$);
this.output$ = combined.pipe(
map(([x, y]) => {
return x.map(a => {
a.title = a.title;
a.id = a.id;
a.other = a.other;
a.exists = y.find(b => b.id === a.id )
});
})
);
在原始代码中,您实质上是将值数组传递为 x
。
请注意find
returns the element found in the array (otherwise undefined). Better use some
。另外,当您 return 地图上的对象时,您应该使用常规 return
语句或用括号括起来的对象。
const combined$ = combineLatest(this.first$, this.second$);
this.output$ = combined.pipe(
map(([x, y]) => {
return x.map(a => {
return {
...a,
exists: y.some(b => b.id === a.id)
};
});
})
);
我有两个 Observable<MyType>
export interface MyType{
title: string;
id: string;
other: [];
}
我想向第一个数组添加额外的 属性 exists
并将其设置为 true
如果该项目存在于第二个数组中:
const combined$ = combineLatest(this.first$, this.second$);
this.output$ = combined.pipe(
map((x, y) => {
return x.map(a => {
a.title = a.title;
a.id = a.id;
a.other = a.other;
a.exists = y.find(b => b.id === a.id )
});
})
);
如果订阅 output
observable this.output$.subscribe(console.log);
[...undefined]
结果
有什么解决办法吗?
我认为组合发送一个值,它是一组单独的值。这里 y
将是未定义的。
使用 ([x,y]) 解构 map 中的值,然后重试。
combined$ 也有一个你漏掉的拼写错误。
并且 find
可以替换为 some
以更好地表示逻辑和 return 布尔值
此外,当您使用 x.map
时,您在逻辑上映射了错误的数组。
const combined$ = combineLatest(this.first$, this.second$);
this.output$ = combined$.pipe(
map(([x, y]) => {
return x.map(a => {
a.title = a.title;
a.id = a.id;
a.other = a.other;
a.exists = y.some(b => b.id === a.id )
});
})
);
在你的代码片段中,你有一个拼写错误,你将 combinedLatest rxjs 运算符的结果设置为 combined$
,然后你在下一行将其称为 combined
,我认为这是不正确的,或者只是将此问题转换为 SO 时的翻译错误。 (不管怎样,一定要指出来嘿嘿)
接下来,combineLatest
运算符 returns 所有可观察值的数组。因此,您可以在 map
运算符中使用解构轻松地从所有可观察对象中获取最新值。
下面是最终代码:
const combined$ = combineLatest(this.first$, this.second$);
this.output$ = combined.pipe(
map(([x, y]) => {
return x.map(a => {
a.title = a.title;
a.id = a.id;
a.other = a.other;
a.exists = y.find(b => b.id === a.id )
});
})
);
在原始代码中,您实质上是将值数组传递为 x
。
请注意find
returns the element found in the array (otherwise undefined). Better use some
。另外,当您 return 地图上的对象时,您应该使用常规 return
语句或用括号括起来的对象。
const combined$ = combineLatest(this.first$, this.second$);
this.output$ = combined.pipe(
map(([x, y]) => {
return x.map(a => {
return {
...a,
exists: y.some(b => b.id === a.id)
};
});
})
);