0

Group to Category Conversion


var Auto = ['new_auto', 'add_auto'];
//more category objects 

var categories = [Auto, Fire, Health, Life, Bank];

function groupToCat(group) {

    for (x = 1; x < categories.length; x++) {
        for (i = 1; i < categories[x].length) {
            if (group == categories[x][i]) {
                return categories[x]
            }
        }
    }
}

I'm trying to use a loop within a loop in combination with a multi-dimensional array to achieve a neat conversion function from a group (string new_auto) to a string equal to the name of the category containing it (array object Auto).


But as it is, I'm returning the category object, not its name. How can I do this?

  • is this what you are after? http://stackoverflow.com/questions/1009911/javascript-get-argument-value-and-name-of-passed-variable – Kirby Mar 18 '14 at 21:58
  • @Kirby ... That's almost funny. In a language where you can do 'just about' anything that you can imagine, you CAN'T convert an object to a string containing the name representing it. Well that's just my luck. –  Mar 18 '14 at 22:01
  • 1
    Rather than try to return the variable name (`Auto`) I'd suggest you make the category name a property of the object and return that. `var Auto = { name: 'Auto', items: ['new_auto', 'add_auto'] };` then `return categories[x].name`. Also I think you need to start with 0, not 1, when looping over those arrays. – palmsey Mar 18 '14 at 22:01
  • @palmsey Ooh, perfect solution. And yep. Newbie mistake. –  Mar 18 '14 at 22:02
  • http://jsfiddle.net/eaeCF/ – adeneo Mar 18 '14 at 22:04
  • @adeneo see my self-answer. I've been shown that solution, and it works nicely for a one-level conversion, but for the sake of dynamic functionality, I went with that approach. If I wanted to use sub-groups as well, for example, I don't see how the quick `indexOf()` trick would work. –  Mar 18 '14 at 23:38

1 Answers1

0

Dynamic item "de-categorization" function


As suggested by Palmsey in the comments, this is the proper way of discovering an object's parent category through the combination of a loop-within-a-loop and a multidimensional array.

//Define Category Names and Children

var Auto = {
    name: 'Auto',
    items: ['new_auto', 'add_auto']
};

var Fire = {
    name: 'Fire',
    items: ['new_fire', 'add_fire']
};

var Health = {
    name: 'Health',
    items: ['health']
};

//Bring each category into a single array

var categories = [Auto, Fire, Health, Life, Bank];

//Because of the complimentary nature of loops and arrays, we can
//access each additional dimension of the array with an additional
//loop dimension.

function groupToCat(group) {

    for (x = 0; x < categories.length; x++) {
        for (i = 0; i < categories[x].items.length; i++) {
            if (group == categories[x].items[i]) {
                return (categories[x].name);
            }
        }
    }

    return ("No match found!");
}

I chose this method above the alternatives because it's dynamic. Simply by continuing the pattern, we can convert the values of items to the groups that they belong to, and vice-versa, with any level of group complexity.