I have two Observable arrays type of Observable<MyType>
export interface MyType{
title: string;
id: string;
other: [];
}
I want to add additional property exists
to the first array and set it to true
if the item is exists in the second array:
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 )
});
})
);
Always getting [...undefined]
result if subscribing to the output
observable this.output$.subscribe(console.log);
Any ideas how to fix?
Please be aware that find
returns the element found in the array (otherwise undefined). Better use some
. Plus, when you return the object on the map, you should use either a regular return
statement or an object enclosed in parenthesis.
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)
};
});
})
);