1.js的sort()方法
使用方法:
(1).ArrObject.sort();
若sort()函数没有参数,则按数组中的字母顺序排序,即字符编码顺序排序,因此如果要进行排序,应先将数组元素转变为字符串再进行排序。
sort()函数默认升序排序。
(2).ArrObject.sort(sortby);
sort函数也可以接收参数,但参数sortby必须是一个函数。
var arr = [1,2,3];
调用sort()函数对arr进行排序
arr.sort(sortby);
function sortby(a,b){ //升序排序 return a - b; //降序 // return b - a; }
(3).sort()函数按某一属性对json数组排序
json数组
var imgData = {"data":[{"src":"26.jpg","author":"太郎","date":"03-12","num":123},{"src":"27.jpg","author":"xiaotaio","date":"03-13","num":1563},{"src":"29.jpg","author":"筱筱","date":"03-12","num":5663},{"src":"83.jpg","author":"小哈就","date":"03-12","num":454},{"src":"30.jpg","author":"成本撒阿萨","date":"03-12","num":765},{"src":"38.jpg","author":"石帆胜丰","date":"03-12","num":5643},{"src":"50.jpg","author":"大范甘迪","date":"03-12","num":655}]} console.log(imgData.data.sort(numDown('num')));
sortNum函数
function sortNum(property){ return function numDown(arr,brr){ var value1 = arr[property]; var value2 = brr[property]; return value2 - value1; } }
2.js中变量加var和不加var的区别
https://blog.csdn.net/hero_xsx/article/details/81562055
3.作用域和作用域链
4.闭包