1.获取当前时间
// 获取当前时间
getNowTime() {
const yy = new Date().getFullYear()
const MM = (new Date().getMonth() + 1) < 10 ? '0' + (new Date().getMonth() + 1) : (new Date().getMonth() + 1)
const dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
const HH = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours()
const mm = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes()
const ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds()
return yy + '-' + MM + '-' + dd + ' ' + HH + ':' + mm + ':' + ss
}
2. JS检测数组中重复出现的数据及其位置(很好用)
2.1 数组中是数字或者字符串
function findDuplicates(arr) {
const duplicates = {};
arr.forEach((item, index) => {
if (arr.indexOf(item) !== index) {
duplicates[item] = duplicates[item] || [];
duplicates[item].push(index);
}
});
return duplicates;
}
// 示例使用
const myArray = [1, 3, 2, 4, 2, 3, 5];
const duplicates = findDuplicates(myArray);
console.log(duplicates); // 输出: { 2: [2, 5], 3: [1, 4] }
2.2 数组中是json
// JS检测数组中重复出现的数据及其位置(json)
findDuplicates(arr) {
let duplicates = {};
arr.forEach((item, index) => {
if (!duplicates[item.name) {
duplicates[item.name] = [];
}
duplicates[item.name].push(index);
});
return duplicates;
}
arr=[{name:'zs',age:18},{name:'ls', age:18},{name:'ww',age:20},{name:'zs',age:18}]
console.log(this.findDuplicates(arr))
最后打印:
数组长度代表在数组中出现了几次,数组代表在数组中的位置