单条件
function dataGroupingToArray(array, key, resKey = "list") {
if (!key) return array;
var aMap = [];
var aResult = [];
for (var i = 0; i < array.length; i++) {
var item = array[i];
if (aMap.indexOf(item[key]) === -1) {
var oItem = {};
oItem[resKey] = [item];
oItem[key] = item[key];
aResult.push(oItem);
aMap.push(item[key]);
} else {
var index = aMap.indexOf(item[key]);
aResult[index][resKey].push(item);
}
}
return aResult;
}
//调用
this.dataGroupingToArray(this.list,'bloodBreedId')
多条件
dataGroupingToArray(array, keyArry, resKey = 'list') {
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 repetitionKey = ''
keyArry.forEach((keyValue) => {
repetitionKey = repetitionKey + keyValue
repetitionValue += '' + item[keyValue]
})
if (aMap.indexOf(repetitionValue) === -1) {
//没有重复
var oItem = {}
oItem[resKey] = [item]
oItem[repetitionKey] = repetitionValue //item[key] //分组的字段
aResult.push(oItem)
aMap.push(repetitionValue)
} else {
//重复了
var index = aMap.indexOf(repetitionValue)
aResult[index][resKey].push(item)
}
}
return aResult
},
//调用
this.dataGroupingToArray(this.list, ['bloodBreedId', 'bloodType'])