groupDepart(arr){ var map = {}, dest = []; for(var i = 0; i < arr.length; i++){ var ai = arr[i]; if(!map[ai.departCode]){ dest.push({ departCode: ai.departCode, dname: ai.dname, data: [ai] }); map[ai.departCode] = ai; }else{ for(var j = 0; j < dest.length; j++){ var dj = dest[j]; if(dj.departCode == ai.departCode){ dj.data.push(ai); break; } } } } console.log(dest,'.....') },
经常遇到的一个写法