JavaScript & jQuery Code Snippet
1. 按照每个object的Name属性对object对象集合进行排序;
//sort a collection of objects by Name property function sortByItemName(originalObjects) { var options = {}; $.each(originalObjects, function (index, item) { options[item.Name] = item; }); //sort() is case sensitive by default var keys = Object.keys(options).sort(function (a, b) { return a.toLowerCase().localeCompare(b.toLowerCase()); }); //get the results after sort var sortedObjects = {}; $.each(keys, function (i, key) { sortedObjects[i] = options[key]; }); return sortedObjects; }
2. JavaScript中获取parameter的方法:
function getQueryStrings(){ var vars = [], hash; var q = document.URL.split('?')[1]; if(q != undefined){ q = q.split('&'); for(var i = 0; i < q.length; i++){ hash = q[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } } return vars; }
另一种方法:
//name参数为需要查询的querystring的key function getQueryStringRegExp(name) { var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i"); if (reg.test(location.href)) return unescape(RegExp.$2.replace(/\+/g, " ")); return ""; }
3.