views:

30

answers:

1

Please forgive me, but I do know js basics and how to write/call a basic function, but in this case I'm trying to match an alpha sorted list of categories from the DB to match against my set (non-alpha) order of said categories as specified below (this is code suggested for use by another user on a question I asked about how to map the order of categories being returned). However, I can't figure out how to integrate that person's code answer into my own code.

Just seeing it implemented against another example list (to mimic what I would grab from the DB) and how I would call it in a switch case statement to wrap my html around the category names and return them to the web page in the mapped order I want would help a great deal. Sorry if it turns out to be obvious. I'm trying to learn what I can as I encounter these problems. Thanks!

var categories = [ 'F', 'C', 'A1', 'A2', 'A3' ].map(function (category) {
    return businesses.filter(function (business) {
        return business.category === category;
    });
});
+2  A: 

The code given looks most likely to be using the jQuery JavaScript library that has some useful functions such as map() for manipulating arrays.

The code assumes that you have a jQuery object / array, businesses that contains the businesses that you need to order. If we read it from the outside in

  1. pass each item in the array in turn into the map function. The map function takes a function to say how each item should be mapped.
  2. The anonymous function function (category)... accepts one argument, the particular item passed to it in this iteration and will return the mapping result.
  3. the result returned is a function of calling filter on businesses and passing it a function to say how businesses should be filtered.
  4. The anonymous function function (business)... accepts an argument, the particular item passed to it as businesses is iterated and returns those that match the category argument scoped in the outer function.
  5. The end result is an array of items ordered by the category array [ 'F', 'C', 'A1', 'A2', 'A3' ]

If we go back to the original problem, you need to order a list of categories based on the client's preference. Let's create a an object literal to map the ordering

var map = {
    F : 5,
    C : 3,
    A1 : 1,
    A2 : 4,
    A3 : 2
}

We can use this map to order the array using the sort method

var array = ['F', 'C', 'A1', 'A2', 'A3'];

array.sort(function(a,b) {
    return map[a] - map[b];
});

This returns us ["A1", "A3", "C", "A2", "F"]

Russ Cam
Thanks so much Russ. This was the BIG missing step as it turns out. In the original problem, it sounded like I could just implement their answer without any use of a library...and I wrestled with that for an hour and was further confused by my google search on filter and map functions, etc.. Anyway, this was clear, concise and I feel slightly less embarrassed for having asked now. This also was a great lesson in array mapping!!!! Cheers!
Lynn