时间紧急,帮人写的…
var searchBy = [{ searchby: "country", value: "UK", type: "term" }, { searchby: "name", value: "Lucy", type: "term" }, { searchby: "country", value: "China", type: "term" }, { searchby: "name", value: "Tom", type: "term" }, { searchby: "startDate", value: "1991-04-05", type: "range" }, { searchby: "number", value: "01", type: "term" }, { searchby: "number", value: "02", type: "term" }, { searchby: "startDate", value: "1991-04-08", type: "range" } ]; var result = GetAggregationResult(searchBy); debugger; ////////////////////////////////////////////////////// function GetAggregationResult(searchBy) { if (!searchBy || searchBy.length < 1) { return []; } var result = []; GetType(searchBy).forEach(function (type) { result = result.concat(GetValue(type)); }); return result; function GetValue(typeBy) { var tempResult = []; typeBy.set.forEach(function (searchItem) { var targetSearchBySet = null; for (var i = 0; i < tempResult.length; i++) { if (tempResult[i].searchby == searchItem.searchby) { targetSearchBySet = tempResult[i].valueSet; break; } } if (targetSearchBySet) { if (targetSearchBySet.indexOf(searchItem.value) == -1) { targetSearchBySet.push(searchItem.value); } } else { tempResult.push({ searchby: searchItem.searchby, valueSet: [searchItem.value] }); } }); var result = []; tempResult.forEach(function (element) { var item = {}; var searchByItem = {}; searchByItem[element.searchby] = element.valueSet; item[typeBy.typeName] = searchByItem; result.push(item); }); return result; } function GetType(searchBy) { var result = []; searchBy.forEach(function (searchItem) { var currentType = searchItem.type; var targetTypeSet = null; for (var i = 0; i < result.length; i++) { if (result[i].typeName == currentType) { targetTypeSet = result[i].set; break; } } if (targetTypeSet) { targetTypeSet.push(searchItem); } else { result.push({ typeName: currentType, set: [searchItem] }); } }); return result; } }