diff --git a/src/utils/commons.js b/src/utils/commons.js index d661781..271e6c7 100644 --- a/src/utils/commons.js +++ b/src/utils/commons.js @@ -290,17 +290,32 @@ export function set_array_index(result) { } /** - * 排序 + * 数组排序 */ export const sortBy = (key) => { return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0); }; +/** + * Object排序keys + */ export const sortKeys = (obj) => Object.keys(obj) .sort() .reduce((a, k2) => ({...a, [k2]: obj[k2]}), {}); +/** + * 数组排序, 给定排序数组 + * @param {array} items 需要排序的数组 + * @param {array} keyName 排序的key + * @param {array} keyOrder 给定排序 + * @returns + */ +export const sortArrayByOrder = (items, keyName, keyOrder) => { + return items.sort((a, b) => { + return keyOrder.indexOf(a[keyName]) - keyOrder.indexOf(b[keyName]); + }); +}; /** * 合并Object, 递归地 */ @@ -341,7 +356,7 @@ export function merge(...objects) { */ export function groupBy(array, callback) { return array.reduce((groups, item) => { - const key = callback(item); + const key = typeof callback === 'function' ? callback(item) : item[callback]; if (!groups[key]) { groups[key] = [];