common utils

feature/hotel-cruise
Lei OT 2 years ago
parent 9718c023a0
commit 606c1e9983

@ -290,17 +290,32 @@ export function set_array_index(result) {
} }
/** /**
* 排序 * 数组排序
*/ */
export const sortBy = (key) => { export const sortBy = (key) => {
return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0); return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0);
}; };
/**
* Object排序keys
*/
export const sortKeys = (obj) => export const sortKeys = (obj) =>
Object.keys(obj) Object.keys(obj)
.sort() .sort()
.reduce((a, k2) => ({...a, [k2]: obj[k2]}), {}); .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, 递归地 * 合并Object, 递归地
*/ */
@ -341,7 +356,7 @@ export function merge(...objects) {
*/ */
export function groupBy(array, callback) { export function groupBy(array, callback) {
return array.reduce((groups, item) => { return array.reduce((groups, item) => {
const key = callback(item); const key = typeof callback === 'function' ? callback(item) : item[callback];
if (!groups[key]) { if (!groups[key]) {
groups[key] = []; groups[key] = [];

Loading…
Cancel
Save