判断实体对象是否为空
Object.keys(object).length === 0;//如果实体对象是 let object={},则返回为0 反之 let object = {a:'1'},则返回1,结果以object的属性count为准
导出数据生成excel
//需要引用
import * as XLSX from "xlsx";
import { saveAs } from "file-saver";
exptDiffData() {
if (!this.data|| this.data.length == 0) {
this.$message.error("没有数据导出!");
return;
}
// 将数据转换为工作表
const worksheet = XLSX.utils.json_to_sheet(this.grdSurData);
// 创建工作簿并添加工作表
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");
// 生成Excel文件
const excelBuffer = XLSX.write(workbook, { bookType: "xls", type: "array" });
// 使用blob和FileReader创建一个URL然后下载
const dataBlob = new Blob([excelBuffer], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8" });
saveAs(dataBlob, "myExcel.xls");
this.$message.success("保存成功");
}
以时间生成一个字符串
formatDateTime() {
let date = new Date();
const year = date.getFullYear();
const month = this.padZero(date.getMonth() + 1);
const day = this.padZero(date.getDate());
const hours = this.padZero(date.getHours());
const minutes = this.padZero(date.getMinutes());
const seconds = this.padZero(date.getSeconds());
return `${year}${month}${day}${hours}${minutes}${seconds}`;
},
取数组中重复的数据(复杂数组)
getRepeatData(data) {
let columnName="你要查重的属性";
let nameCount = data.reduce((acc, curr) => {
acc[curr[columnName]] = (acc[curr[columnName]] || 0) + 1;
return acc;
}, {});
// 获取重复的 name
return Object.keys(nameCount).filter((name) => nameCount[name] > 1);
},
评论区