1

使用normalizr库后,我的应用程序的 Redux 状态中有以下标准化 JSON 对象结果:

  { 
    sports: {
        byId: {
          1: {
            id: 1,
            name: 'Soccer',
            slug: 'soccer'
          },
          2: {
            id: 2,
            name: 'Basketball',
            slug: 'basketball'
          },
          3: {
            id: 3,
            name: 'American Football',
            slug: 'american-football'
          }
        },
        allIds: [
          '1',
          '2',
          '3'
        ]
      },
      competitions: {
        byId: {
          '1': {
            id: 1,
            name: 'Competition 1',
            short_name: 'Comp 1',
            slug: 'comp-1',
            sport: 1,
            status: {
             is_live: false 
           }
          },
          '2': {
            id: 2,
            name: 'Competition 2',
            short_name: 'Comp 2',
            slug: 'comp-2',
            sport: 1,
            status: {
             is_live: true 
           }
          },
          '3': {
            id: 3,
            name: 'National Basketball League',
            short_name: 'NBA',
            slug: 'national-basketball-league',
            sport_slug: 'basketball',
            sport: 3,
            status: {
             is_live: true 
           }
          }
        },
        allIds: [
          '1',
          '2',
          '3'
        ]
     }

我想要实现的目标:我想要一个按 .competitions过滤/分类的列表sports

我怎样才能做到这一点?

我也希望能够competitions按它的status.is_live.

那么我怎样才能得到一个等于真和等于假的细分列表呢competitionssportstatus.is_livecompetitions status.is_live

任何帮助表示赞赏!谢谢

4

1 回答 1

0

如果您不想使用lodash,您可以很容易地编写一个.groupBy函数(示例)。您需要遍历输出对象并使用 for 而不是使用 重新分配其子值.mapValues

我在示例中使用了 lodash,只是为了指出逻辑。

注意:我会删除原始响应中数据中的分组,并让客户端自己执行此操作 - 处理未排序的数组和过滤器/映射更容易,而不是处理来自具有冗余的对象的值键(因为它们按 Id 表示组)

let data = {
  sports: {
    byId: {
      1: {
        id: 1,
        name: 'Soccer',
        slug: 'soccer'
      },
      2: {
        id: 2,
        name: 'Basketball',
        slug: 'basketball'
      },
      3: {
        id: 3,
        name: 'American Football',
        slug: 'american-football'
      }
    },
    allIds: [
      '1',
      '2',
      '3'
    ]
  },
  competitions: {
    byId: {
      '1': {
        id: 1,
        name: 'Competition 1',
        short_name: 'Comp 1',
        slug: 'comp-1',
        sport: 1,
        status: {
          is_live: false
        }
      },
      '2': {
        id: 2,
        name: 'Competition 2',
        short_name: 'Comp 2',
        slug: 'comp-2',
        sport: 1,
        status: {
          is_live: true
        }
      },
      '3': {
        id: 3,
        name: 'National Basketball League',
        short_name: 'NBA',
        slug: 'national-basketball-league',
        sport_slug: 'basketball',
        sport: 3,
        status: {
          is_live: true
        }
      }
    },
    allIds: [
      '1',
      '2',
      '3'
    ]
  }
}

let competitions = Object.values(data.competitions.byId)

// _.chain(arr) keeps returning `lodash` objects
// so I don't have to call it separately for every action
let filteredCompetitions = _.chain(competitions)
  .groupBy(i => i.status.is_live)
  .mapValues(i => _.groupBy(i, 'sport'))
  .value() // returns the final value
  
console.log(filteredCompetitions)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

于 2017-07-30T13:14:48.660 回答