/** * * @param {*} array 分组的数租集合 * @param {*} keyArry 分组字段集合 * @param {*} resKey 分组里的数据自定义字段名称 * @returns */ // 多个字段分组 function dataGroupingToArray(array, keyArry, resKey = 'data') { if (keyArry.length <= 0) return array var aMap = [] var aResult = [] for (var i = 0; i < array.length; i++) { var item = array[i] var repetitionValue = '' var SJCS = '' var LJZH = '' keyArry.forEach((keyValue) => { if(keyValue == 'SJCS'){ SJCS = item[keyValue]; } if(keyValue == 'LJZH'){ LJZH = item[keyValue]; } repetitionValue += '' + item[keyValue]; }) if (aMap.indexOf(repetitionValue) === -1) { //没有重复 var oItem = {} oItem[resKey] = [item] oItem['SJCS'] = SJCS; oItem['LJZH'] = LJZH; aResult.push(oItem) aMap.push(repetitionValue) } else { //重复了 var index = aMap.indexOf(repetitionValue) aResult[index][resKey].push(item) } } return aResult } // 使用方法 var newlist = dataGroupingToArray(array, ['LJZH', 'SJCS'])