小编Kis*_*ani的帖子

如何将distinct()应用于对象数组RxJS?

我正在从 swagger pets API 中获取宠物列表。我想删除具有重复 id 的宠物,然后将其发送到 React 以避免重复密钥问题。我尝试了下面的代码,但对我不起作用。谁能帮我解决这个问题?

我是 RxJS 的新手,所以想删除基于宠物 ID 过滤不同对象的注释代码。

export function fetchPetEpic(action$) {
    return action$.pipe(
        ofType('GET_PETS'),
        switchMap(action => 
            ajax.getJSON(`https://petstore.swagger.io/v2/pet/findByStatus?status=${action.payload}`).pipe(
                map( response => response.map( (pet) => ({
                    id: pet.id,
                    pet: pet.pet
                }))),
                distinct( petList => petList.id),
                // map( petList => petList.filter((pet, index, list) => {
                //                     return list.map(petObj =>
                //                         petObj.id).indexOf(pet.id) === index;
                //                     })),
                map(response => petActions.setPets(response))
        )),
        catchError( error => ({ type: 'ERROR_PETS_FETCH', payload: error})),
    )
}
Run Code Online (Sandbox Code Playgroud)

假设 pets 数组类似于 [ { id: 1, …

redux-observable rxjs6

2
推荐指数
1
解决办法
4266
查看次数

标签 统计

redux-observable ×1

rxjs6 ×1