1

after use normalizr library I have the following normalized JSON object result in the Redux state of my application:

  { 
    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'
        ]
     }

What I want achieve: I want a list of competitions filtered/categorized by sports.

How can I do that?

Also I want to be able to group competitions by it's status.is_live.

So how can I get an list of competitions breakdown by sport that status.is_live equals true and competitions status.is_live equals false?

Any help is appreciated! Thanks

1 Answers1

0

If you don't want to use lodash, you can write a .groupBy function pretty easily (example). You'll need to loop over the output object and reassign its children values with a for instead of using .mapValues.

I used lodash in the example, just to point out the logic.

Note: I'd remove the grouping in the data in the original response, and let the client do this on its own - it's easier to work on an unsorted array and filter/map that instead of working against values from an object that has redundant keys (since they represent groups by 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>
casraf
  • 21,085
  • 9
  • 56
  • 91
  • Appreciate your help. I tried this and I think it works without have status/is_live reference on `sport`. `let filteredCompetitions = _.chain(competitions) .groupBy('sport') .value() // returns the final value _.forEach(filteredCompetitions, function(value, key) { filteredCompetitions[key] = _.groupBy(filteredCompetitions[key], function(item) { return item.status.is_live; }); }); console.log(filteredCompetitions)` Maybe have more elegant way to do that ? – Alexandre Moura Jul 30 '17 at 13:49