Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
  
</body>
</html>
 
// The list of all sub categories
myList = [".a1", ".b1", ".b3", ".d2", ".e2", ".e4", ".e5"];
// The function to get the category of a particular subcategory
function getCategory(subCat){
    return subCat.substr(1, 1);
}
// The function to build our selector recursively
function buildSelector(categoryList, categoryIndexArray, categoryIndex)
{
    if (categoryIndex == categoryIndexArray.length - 1)
        return categoryList[categoryIndexArray[categoryIndex]];
    else {
        var results = [];
        var subList = buildSelector(categoryList, categoryIndexArray, categoryIndex + 1);
        $.each(categoryList[categoryIndexArray[categoryIndex]],function(_,e){
            $.each(subList,function(_,e1){
                results.push(e + e1);
            });
        });
        return results;
    }
}
var categories = {}; 
var categoryArray = [];
// build the categories and categoryArray 
$.each(myList,function(_,e){
    var cat = getCategory(e);
    if (categories[cat] === undefined)
    {
        categories[cat] = [];
        categoryArray.push(cat);
    }
    if (categories[cat].indexOf(e) < 0)
        categories[cat].push(e);
});
// build our selector as array of groups
var selectors = buildSelector(categories, categoryArray, 0);
// join all selector groups to get the string form
console.log(selectors.join(","));
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
trunghieulepro
0viewers