|
|
@@ -27,7 +27,7 @@ export const sleep = (ms: number) => { return new Promise((resolve) => { setTime
|
|
|
* @param arr
|
|
|
* @returns
|
|
|
*/
|
|
|
-export const flatten:any = (arr: Array<Any>) => {
|
|
|
+export const flatten: any = (arr: Array<Any>) => {
|
|
|
return [].concat(
|
|
|
...arr.map((x) => (Array.isArray(x) ? flatten(x) : x))
|
|
|
)
|
|
|
@@ -55,24 +55,24 @@ export const search = (data: Array<Any>, key: string) => {
|
|
|
* @param fmt 日期格式
|
|
|
* @returns
|
|
|
*/
|
|
|
- export const fomartTime = (date: Date, fmt = 'yyyy-MM-dd hh:mm:ss') => {
|
|
|
+export const fomartTime = (date: Date, fmt = 'yyyy-MM-dd hh:mm:ss') => {
|
|
|
if (!date) return ''
|
|
|
const o = {
|
|
|
- "M+": date.getMonth() + 1, //月份
|
|
|
- "d+": date.getDate(), //日
|
|
|
- "h+": date.getHours(), //小时
|
|
|
- "m+": date.getMinutes(), //分
|
|
|
- "s+": date.getSeconds(), //秒
|
|
|
- "q+": Math.floor((date.getMonth() + 3) / 3), //季度
|
|
|
- "S": date.getMilliseconds() //毫秒
|
|
|
+ "M+": date.getMonth() + 1, //月份
|
|
|
+ "d+": date.getDate(), //日
|
|
|
+ "h+": date.getHours(), //小时
|
|
|
+ "m+": date.getMinutes(), //分
|
|
|
+ "s+": date.getSeconds(), //秒
|
|
|
+ "q+": Math.floor((date.getMonth() + 3) / 3), //季度
|
|
|
+ "S": date.getMilliseconds() //毫秒
|
|
|
} as any
|
|
|
if (/(y+)/.test(fmt)) {
|
|
|
- fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
|
|
+ fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
|
|
|
}
|
|
|
for (let k in o) {
|
|
|
- if (new RegExp("(" + k + ")").test(fmt)) {
|
|
|
- fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
|
|
- }
|
|
|
+ if (new RegExp("(" + k + ")").test(fmt)) {
|
|
|
+ fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
|
|
|
+ }
|
|
|
}
|
|
|
return fmt;
|
|
|
}
|
|
|
@@ -84,7 +84,7 @@ export const search = (data: Array<Any>, key: string) => {
|
|
|
*/
|
|
|
export const mileage2string = (num: number) => {
|
|
|
const a = Math.floor(num / 1000).toString()
|
|
|
- const ab =parseFloat((num % 1000).toFixed(1))
|
|
|
+ const ab = parseFloat((num % 1000).toFixed(1))
|
|
|
const b = Math.floor(num % 1000).toString()
|
|
|
const c = b.length === 1 ? `00${ab}` : b.length === 2 ? `0${ab}` : ab
|
|
|
return `${a}+${c}`
|
|
|
@@ -108,3 +108,23 @@ export const uniqueArr = <T = Any>(arr: T[], key: keyof T) => {
|
|
|
}
|
|
|
return newArr
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数组按指定key值分组
|
|
|
+ * @param {*} array
|
|
|
+ * @param {*} id
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+export const groupBy = (array: Array<any>, id: string) => {
|
|
|
+ let groups = {} as any;
|
|
|
+ array.forEach((o) => {
|
|
|
+ let group = JSON.stringify(o[id]);
|
|
|
+ if (typeof o[id] === 'string') {
|
|
|
+ group = o[id]
|
|
|
+ }
|
|
|
+ groups[group] = groups[group] || [];
|
|
|
+ groups[group].push(o);
|
|
|
+ });
|
|
|
+ // return Object.values(groups);
|
|
|
+ return groups;
|
|
|
+}
|