刘洋博客
猥琐发育别浪
js日常记录
admin (2023-10-13)|222次阅读

js日常记录

数组去重

// 第一种:Map记录
function quchong1(arr) {
    const newArr = []
    arr.reduce((pre, next) => {
        if (!pre.has(next)) {
            pre.set(next, 1)
            newArr.push(next)
        }
        return pre
    }, new Map())
    return newArr
}
 
// 第二种:Set去重
function quchong2(arr) {
    return [...new Set(arr)]
}

实现(a == 1 && a == 2 && a == 3)为true

// 第一种方法
var a = {
  i: 1,
  toString: function () {
    return a.i++;
  }
}
console.log(a == 1 && a == 2 && a == 3) // true
 
// 第二种方法
var a = [1, 2, 3];
a.join = a.shift;
console.log(a == 1 && a == 2 && a == 3); // true
 
// 第三种方法
var val = 0;
Object.defineProperty(window, 'a', {
    get: function () {
        return ++val;
    }
});
console.log(a == 1 && a == 2 && a == 3) // true

耗时统计

var times = function() {
    var timing = performance.timing;
    var loadTime = timing.loadEventEnd - timing.navigationStart;//过早获取时,loadEventEnd有时会是0
    if(loadTime <= 0) {
    // 未加载完,延迟200ms后继续times方法,直到成功
        setTimeout(function(){
            times();
        }, 200);
        return;
    }
    var readyStart = timing.fetchStart - timing.navigationStart;
    var redirectTime = timing.redirectEnd  - timing.redirectStart;
    var appcacheTime = timing.domainLookupStart  - timing.fetchStart;
    var unloadEventTime = timing.unloadEventEnd - timing.unloadEventStart;
    var lookupDomainTime = timing.domainLookupEnd - timing.domainLookupStart;
    var connectTime = timing.connectEnd - timing.connectStart;
    var requestTime = timing.responseEnd - timing.requestStart;
    var initDomTreeTime = timing.domInteractive - timing.responseEnd;
    var domReadyTime = timing.domComplete - timing.domInteractive; //过早获取时,domComplete有时会是0
    var loadEventTime = timing.loadEventEnd - timing.loadEventStart;

    // 为console.table方法准备对象,包含耗时的描述和消耗的时间
    var allTimes = [
        { "描述": "准备新页面时间耗时", "时间(ms)": readyStart },
        { "描述": "redirect 重定向耗时", "时间(ms)": redirectTime },
        { "描述": "Appcache 耗时", "时间(ms)": appcacheTime },
        { "描述": "unload 前文档耗时", "时间(ms)": unloadEventTime },
        { "描述": "DNS 查询耗时", "时间(ms)": lookupDomainTime },
        { "描述": "TCP连接耗时", "时间(ms)": connectTime },
        { "描述": "request请求耗时", "时间(ms)": requestTime },
        { "描述": "请求完毕至DOM加载", "时间(ms)": initDomTreeTime },
        { "描述": "解释dom树耗时", "时间(ms)": domReadyTime },
        { "描述": "load事件耗时", "时间(ms)": loadEventTime },
        { "描述": "从开始至load总耗时", "时间(ms)": loadTime }
    ];
    console.table(allTimes);
}
window.onload = times;// onload时,触发times方法

比较2个数组

// `a` 和 `b` 都是数组
const isEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);

// 或者
const isEqual = (a, b) => a.length === b.length && 
  a.every((v, i) => v === b[i]);

// 事例
isEqual([1, 2, 3], [1, 2, 3]); // true
isEqual([1, 2, 3], [1, '2', 3]); // false

获取选中文本

const getSelectedText = () => window.getSelection().toString();

小于10前面+0

var formatNumber = function(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
  // 或者 return ('0'+ n).slice(-2)
}

计算坐标之间的距离

function getDistance(lng1, lat1, lng2, lat2) {
    let alpha = (lat1 * Math.PI) / 180.0;
    return Math.sqrt(Math.pow((lat1 - lat2) * 111.12, 2) + Math.pow((lng1 - lng2) * Math.cos(alpha) * 111.12, 2));
}