更新一点点知识

476 阅读1分钟

控制台输入一下文本,拾色器

await new EyeDropper().open() 

数组过滤 '' null NaN false undefined 0

arr.filter(Boolean)

26个大写字母

Array.from(new Array(26), (x, i) => String.fromCharCode(65 + i));

26个小写字母

Array.from(new Array(26), (x, i) => String.fromCharCode(97 + i));

小数转换百分比

  • 0.285转换百分比,直接*100,会无尽小数位

function numToPercent(num) {
  if (!num || Number.isNaN(Number(num))) {
    return 0;
  }
  return num >= 1 ? 100 : Math.round(num * 10000) / 100;
}

数组对象是否有重复

const repeat = list.some((item, index, array) => {
    return array.filter(innerItem => innerItem.key === item.key).length > 1;
  });

数字转换千分位

 Number(num).toLocaleString();

判断刘海屏


function isNotchDevice() {
    // 使用 CSS 媒体查询
    const mediaQuery = window.matchMedia('(env(safe-area-inset-top))');
    if (mediaQuery.matches) { return true; }
    
    // 使用 window.safeAreaInsets
    if (window.safeAreaInsets && window.safeAreaInsets.top > 20) { return true; }
    
    // 使用 window.innerWidth 和 window.innerHeight 
    const aspectRatio = window.innerWidth / window.innerHeight;
    if (aspectRatio >= 1.8 && aspectRatio <= 1.9) { return true; }
    
    // 使用 window.visualViewport 
    if (window.visualViewport && window.visualViewport.height < window.innerHeight) { 
        return true; 
    }
    
    return false;
}