反向字符串
const stringReverse = str => str.split("").reverse( ).join("");stringReverse('hello world'); /*dlrow olleh*/
滚动到页面顶部
const scrollToTop = ( ) => window.scrollTo(0, 0);scrollToTop( );
删除数组中的重复项
const removeDuplicate = (arr) => [...new Set(arr)];removeDuplicate([1, 2, 3, 4, 4, 2, 1]); // [1, 2, 3, 4]
获取数组中的随机项
const randomItemArray = (arr) => arr[Math.floor(Math.random( ) * arr.length)];randomItemArray(['a', 'b', 'c', 1, 2, 3]);
获取数组中的最大项
const maxNumber = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);maxNumber([4,9,5,7,2]) /* 9 */
检查型号
function isNumber(num) {return !isNaN(parseFloat(num)) && isFinite(num);}isNumber("Hello"); /*false*/isNumber(123); /*true*/
检查类型为空
const checkNull = val => val === undefined || val === null;checkNull(123) /* false */checkNull() /* true */checkNull('hello') /* false */
获取数组中的最小数
const minNumber = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);console.log(minNumber([3,5,9,7,1])) /*1*/
获取数组中的平均数
const averageNumber = arr => arr.reduce((a, b) => a + b) / arr.length;averageNumber([1, 2, 3, 4, 5]) /* 3 */
检查元素的类型
const checkType = v => v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();checkType(true) /*boolean*/checkType("hello World") /*string*/checkType(123) /*number*/
计算数组中元素的出现次数
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);countOccurrences([1,2,2,4,5,6,2], 2) /* Số 2 xuất hiện 3 lần trong array */
使用Javascript获取当前的URL
const getCurrentURL = () => window.location.href;getCurrentURL() /* https://www.niemvuilaptrinh.com */
大写字符串中的字母
const capitalizeString = str => str.replace(/b[a-z]/g, char => char.toUpperCase());capitalizeString('niem vui lap trinh'); /* 'Niem Vui Lap Trinh' */
将RGB转换为16进制
const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);rgbToHex(52, 45, 125); /* Kết quả là: '#342d7d'*/
将数字转换为数组
const numberToArray = n => [...`${n}`].map(i => parseInt(i));numberToArray(246) /*[2, 4, 6]*/numberToArray(357911) /*[3, 5, 7, 9, 1, 1]*/
从HTML中获取内容
const getTextInHTML = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || '';getTextInHTML('<h2>Hello World</h2>'); /*'Hello World'*/
在JS中分配多个变量
var [a,b,c,d] = [1, 2, 'Hello', false];console.log(a,b,c,d) /* 1 2 'Hello' false */
空数组
let arr = [1, 2, 3, 4, 5];arr.length = 0;console.log(arr); /* Kết quả : [] */
在JS中复制对象
const obj = {name: "niem vui lap trinh",age: 12};const copyObject = { ...obj };console.log(copyObject); /* {name: 'niem vui lap trinh', age: 12}*/
检查偶数和奇数
const isEven = num => num % 2 === 0;console.log(isEven(1)); /*false*/console.log(isEven(2)); /*true*/
合并两个或多个数组JS
const arr1 = [1, 2, 3];const arr2 = [4, 5, 6];const arr = arr1.concat(arr2);console.log(arr); /* [1, 2, 3, 4, 5, 6] */
将内容复制到剪贴板
const copyTextToClipboard = async (text) => {await navigator.clipboard.writeText(text)}
从一系列值中选择一个随机数
var max = 10;var min = 1;var numRandom = Math.floor(Math.random() * (max - min + 1)) + min;console.log(numRandom)
检查元素是否聚焦
const elementFocus = (el) => (el === document.activeElement);elementIsInFocus(element);/*if true element is focus*//*if false element is not focus*/
用JS测试苹果设备
const isAppleDevice =/Mac|iPod|iPhone|iPad/.test(navigator.platform);console.log(isAppleDevice);/*if true element is apple devices **//*if false element is not apple devices*/
将字符串转换为数组
const str = "Hello";const arr = [...str];console.log(arr); /* ['H', 'e', 'l', 'l', 'o'] */
在JS中使用箭头函数
const sum = function(x, y) {return x + y;};/* arrow function */const sum = (x, y) => x + y;