0

I've got nine groups:


newFencepost

oldBoard

bigTree

largeRock

smallPebble

toughBoulder

diamondRing

emeraldNecklace

opalBracelet

and three categories

wood , stone & jewel


Each group belongs to one of the three categories, but the category name is not attached to the group object. So the categories know which groups belong to them, but the groups don't know which categories they belong to.

I need to write a function groupToCat() that takes group as its first parameter and returns the category associated with it.


I could do:

if (group == newFencepost || group == oldBoard || group == bigTree ){
    var category = wood;
};

But that would be ugly, messy, and mean more work each time I add a new category or group.


Being a new programmer, I'd rather start by doing the simple things in the right way. What's a proper way to write this function without having to change it when new content is introduced?

  • Does each `category` have a list of `group`s? So can you do something like `if(wood.groups.indexOf(group) !== -1) return wood;`? – Colin DeClue Mar 04 '14 at 21:00
  • yes, the categories know the groups that belong to them. That information can be attached in any way necessary for the solution. –  Mar 04 '14 at 21:00

1 Answers1

0

I would suggest maintaining a list of categories. So you could do something similar to:

// Assuming all groups were pushed to categories like :
// wood = wood || {groups: []};
// wood.groups.push(bigTree);
// wood.groups.push(newFencepost);
// etc

function groupToCat(group) {
    for(var i = 0; i < categories.length; i++) {
        var category = categories[i];
        // .indexOf on an array will return
        // the index of the passed in object
        // in the array. If it's -1,
        // that means it's not in the array.
        if(category.groups.indexOf(group) !== -1) {
            return category;
        }
    }
}
Colin DeClue
  • 2,194
  • 3
  • 26
  • 47
  • Could you update this explaining how it works? I'm a little confused by the `indexOf(group) !== -1` part –  Mar 04 '14 at 21:07
  • Does that help a little bit? I also added a comment about how you would structure your `category` objects. – Colin DeClue Mar 04 '14 at 21:39
  • I'm studying the `push()` method right now to figure your explanation out –  Mar 04 '14 at 22:10
  • Ok, so you're pushing the group into a category.groups object, which gives it an index of 0 or greater.. Each loop tries a different category. If the indexOf `group` isn't -1, it's a member of that category. Alright. Makes sense. –  Mar 04 '14 at 22:15
  • What does this do? `wood = wood || {groups: []};` –  Mar 04 '14 at 22:33
  • @jt0dd: That says that if wood already exists, we just use that. If it doesn't, we declare it as a new object with a property of `groups` which is an empty array. It's equivalent to `if(wood == null) { wood = {groups:[]}; }` – Colin DeClue Mar 05 '14 at 19:36
  • I only just got around to implementing this kind of function in a script. Could you clarify how the `categories` object should be created? Is it supposed to be an array? –  Mar 15 '14 at 00:18
  • Yes, `categories` should be an array of objects. – Colin DeClue Mar 17 '14 at 16:22
  • Could you take a look at my attempt to utilize this? http://jsfiddle.net/RZ2fQ/ - I'm doing something wrong. –  Mar 18 '14 at 21:10
  • You were close! Just need to move the `categories` definition to below the declaration of the individual categories. I'd also move them out to a separate function (but still have them be global, or at least tied to a global object) so that you don't go through the process of recreating them every time. – Colin DeClue Mar 19 '14 at 15:57
  • Ah, thanks, that was my issue. Also, I tried another approach as well. Do you recommend yours above the one I created here http://stackoverflow.com/questions/22491830/js-category-conversion-function?noredirect=1#comment34218400_22491830 ? –  Mar 23 '14 at 17:51
  • I'd recommend my approach above, as it has one less loop, so is an order of magnitude more efficient. If the second one is easier for you to understand, though, and this won't be called many times with large categories, etc, then that's a fine option as well. – Colin DeClue Mar 24 '14 at 19:05