Jordan
Jordan

Reputation: 2523

Create multiple arrays from one set

I have an array of items, each have an item_type property that could be one of four different types (this is a cart of items). When I attempt to "pay" for the items in the cart, I need to separate all the items remaining into arrays of each item type to send back to the server so the proper tables can be updated. I was just going to use a map to do this but that would require multiple passes (at least the way I was thinking I needed to do it) but is there a quicker way to take one array and split it into multiple arrays based on a property?

Specifically I need to know which array is which (which one has raffle tickets, donations, etc.) so I can post them to the server with the correct property name so the server knows what array is what and how to proceed with each.

Currently in ember this is how I am working through this. Not a lot of code but still I wonder if there is some refactoring that can be done here

// ARRAYS OF CLASSES
itemsArray: Ember.computed.filterBy('model.items','item_type','bid'),
donationsArray: Ember.computed.filterBy('model.items','item_type','donation'),
ticketsArray: Ember.computed.filterBy('model.items','item_type','ticket'),
rafflesArray: Ember.computed.filterBy('model.items','item_type','raffle'),

// ARRAYS OF JUST CLASS IDS
itemIds: Ember.computed.mapBy('itemsArray','id'),
donationIds: Ember.computed.mapBy('donationsArray','id'),
ticketIds: Ember.computed.mapBy('ticketsArray','id'),
raffleIds: Ember.computed.mapBy('rafflesArray','id'),

Upvotes: 0

Views: 66

Answers (1)

Daniel
Daniel

Reputation: 18692

Ok, so first of all your code is absolutely fine.

I was interested in comparing performance of using observer instead of creating 4 computed properties, so I created this twiddle. I haven't found good way to be absolutely sure 1 way is absolutely faster than another, but you here's another way you could use in your code.

createArrays: Ember.on('init', Ember.observer('[email protected]_type', function() {
    console.log('observer fired');
    const items = this.get('model.items'),
          itemsArray = [],
          donationsArray = [],
          rafflesArray = [],
          ticketsArray = [];

    items.forEach(item => {
        switch (Ember.get(item, 'item_type')) {
        case 'bid':
          itemsArray.pushObject(item);
          break;
        case 'donation':
          donationsArray.pushObject(item);
          break;
        case 'ticket':
          ticketsArray.pushObject(item);
          break;
        default:
          rafflesArray.pushObject(item);
          break;
      }
    });

    this.setProperties({
      itemsArray,
      donationsArray,
      ticketsArray,
      rafflesArray
    });
  })),

But I don't think it's worth changing to.

Another thing you could do is programatically create those 8 computed properties, but it would be more code than it is now probably. If you would have 10 instead of 4 item_type's it would be worth to do.

Upvotes: 1

Related Questions