我曾经将两个可观察变量组合为一个,然后获得唯一的唯一值,因此:
const latest = combineLatest(...queryResults, (...arrays) => arrays.reduce((
acc: any,
array: any
) => [...acc, ...array], []));
我有一个数组:
0: {id: "id1", categories: Array(1), value: "Stadium"}
1: {id: "id2", categories: Array(2), value: "ball"}
2: {id: "id3", categories: Array(2), value: "ball"}
3: {id: "id4", categories: Array(1), value: "Woman"}
4: {id: "id5", categories: Array(2), value: "head"}
及之后
return latest.pipe(
map(words => {
return words.reduce((unique, o) => {
if (!unique.some(obj => obj.id === o.id)) {
unique.push(o);
}
return unique;
}, []);
})
);
结果是:
0: {id: "id1", categories: Array(1), value: "Stadium"}
1: {id: "id2", categories: Array(2), value: "ball"}
2: {id: "id4", categories: Array(1), value: "Woman"}
3: {id: "id5", categories: Array(2), value: "head"}
没关系。一切正常。我的问题与控制台错误有关:
ERROR in src/app/core/services/learning.service.ts(137,22): error TS2339: Property 'reduce' does not exist on type '{}'.
第137行->返回words.reduce((unique,o)=> {...
如何解决?
答案 0 :(得分:0)
这基本上是一个数组操作,但是使用rxjs可以用REQUEST_URI
进行简化,但是在应用之前,您将首先合并可观察结果数组,请参见下文
distinctUntilKeyChange