js通過欄位把集合拆分
阿新 • • 發佈:2018-11-10
function GroupFiled(array,filed){
var map = {}, dest = [];
for (var i = 0; i < array.length; i++) {
var ai = array[i];
if (!map[ai[filed]]) {
var param={};
param[filed]=ai[filed];
param.data=[ai];
dest.push(param);
map[ai[filed]] = ai;
} else {
for (var j = 0; j < dest.length; j++) {
var dj = dest[j];
if (dj[filed] == ai[filed]) {
dj.data.push(ai);
break;
}
}
}
}
return dest;
}