如何过滤没有数组的规范化嵌套JSON数据?

时间:2017-07-30 12:55:11

标签: javascript json redux normalizr

使用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过滤/分类。

我该怎么做?

此外,我希望能够通过它competitionsstatus.is_live进行分组。

那么我怎样才能得到competitions sport分析列表,status.is_live等于true而competitions status.is_live等于false?

任何帮助表示赞赏!感谢

1 个答案:

答案 0 :(得分:0)

如果您不想使用lodash,则可以非常轻松地编写.groupBy函数(example)。您需要循环输出对象并使用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>