例如:
arr=[
{name:'a',age:1},
{name:'b',age:2},
{name:'c',age:3},
{name:'d',age:1},
{name:'e',age:1},
{name:'f',age:2},
]
处理结果:sortArr(arr,'age')
arr=[
[{name:'a',age:1}, {name:'d',age:1},{name:'e',age:1}],
[{name:'b',age:2},{name:'f',age:2}],
[{name:'c',age:3}]
]
处理js:
//将一维数组处理成二位数组sortArr(arr, str) {var _arr = [],_t = [],// 临时的变量_tmp;// 按照特定的参数将数组排序将具有相同值得排在一起arr = arr.sort(function (a, b) {var s = a[str],t = b[str];return s < t ? -1 : 1;});if (arr.length) {_tmp = arr[0][str];}// 将相同类别的对象添加到统一个数组for (var i in arr) {if (arr[i][str] === _tmp) {_t.push(arr[i]);} else {_tmp = arr[i][str];_arr.push(_t);_t = [arr[i]];}}// 将最后的内容推出新数组_arr.push(_t);return _arr;}