/* 对象数组转数组 1:obj->Array 2:数据格式
const s ={
a:{label:'1',value:'2'},
b:{label:'1',value:'2'},
...
}
*/
function objToAarr (data){
const newArr =[]
for(const item in data){
newArr.push(data[item])
}
return newArr
}
// 普通数组去重
function uniqueArr(arr) {
return Array.from(new Set(arr))
}
// 数组排序
function sortList(list, key, group) {
const arr = list
arr.sort((a, b) => {
return parseInt(b[key], 10) - parseInt(a[key], 10)
})
return arr
}
// 根据 key 对象数组分组-去重
function uniqueObjArr(list, key) {
const newlist = []
const group = []
list.forEach(element => {
const find = group.indexOf(element[key].replace(/^\s+|\s+$/g, ''))
if (find < 0) {
newlist.push(element)
group.push(element[key].replace(/^\s+|\s+$/g, ''))
}
})
return newlist
}
/** 拷贝*/
function deepClone(source) {
if (!source && typeof source !== 'object') {
throw new Error('error arguments', 'shallowClone')
}
const targetObj = source.constructor === Array ? [] : {}
Object.keys(source).forEach(keys => {
if (source[keys] && typeof source[keys] === 'object') {
targetObj[keys] = deepClone(source[keys])
} else {
targetObj[keys] = source[keys]
}
})
return targetObj
}
// 获取当前时间S
function getTime(type) {
if (type === 'start') {
return new Date().getTime() - 3600 * 1000 * 24 * 90
} else {
return new Date(new Date().toDateString())
}
}
// formatTime当前时间
function formatTime(time, option) {
time = +time * 1000
const d = new Date(time)
const now = Date.now()
const diff = (now - d) / 1000
if (diff < 30) {
return '刚刚'
} else if (diff < 3600) {
// less 1 hour
return Math.ceil(diff / 60) + '分钟前'
} else if (diff < 3600 * 24) {
return Math.ceil(diff / 3600) + '小时前'
} else if (diff < 3600 * 24 * 2) {
return '1天前'
}
if (option) {
return parseTime(time, option)
} else {
return (
d.getMonth() +
1 +
'月' +
d.getDate() +
'日' +
d.getHours() +
'时' +
d.getMinutes() +
'分'
)
}
}