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;
});
});
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,
businessesthat contains the businesses that you need to order. If we read it from the outside infunction (category)...accepts one argument, the particular item passed to it in this iteration and will return the mapping result.businessesand passing it a function to say how businesses should be filtered.function (business)...accepts an argument, the particular item passed to it asbusinessesis iterated and returns those that match the category argument scoped in the outer function.[ '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
We can use this map to order the array using the
sortmethodThis returns us
["A1", "A3", "C", "A2", "F"]