通用
过滤对象数组的重复数据
javascript
export function filterRepeatData(list, key = 'id') {
// 如果每项数据都有id属性,那么可以使用id属性来过滤重复数据
const map = new Map();
list.forEach(item => {
map.set(item[key], item);
});
return Array.from(map.values());
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
去除字符串中的空行和空格
javascript
// 去除空行和空格
export const removeEmptyLine = (str) => {
if (typeof str !== 'string') return '';
return str.replace(/\s*/g, '').replace(/[\r\n]/g, '')
}
1
2
3
4
5
2
3
4
5
判断变量类型
javascript
export function getType(obj) {
const type = Object.prototype.toString.call(obj).slice(8, -1)
// 转为小写
return type.toLowerCase()
}
1
2
3
4
5
2
3
4
5