1. 反转字符串
const reverseString = (str) => str.split("").reverse().join("");
console.log(reverseString("hello guys!"));
// !syug olleh
2. 展平数组
const flattenArray = (arr, flattenLevel = 1) => arr.flat(flattenLevel);
console.log(flattenArray([1, 2, [3, 4]]));
// [1,2,3,4]
console.log(flattenArray([1, 2, [3, 4, [5]]], 2));
// [1,2,3,4, 5]
3. 随机播放数组
const shuffleArray = (arr) => arr.sort((a, b) => 0.5 - Math.random());
console.log(shuffleArray([1, 2, 3, 4, 5]));
// [2, 5, 4, 1, 3]
4. 获取随机布尔值
const getRandomBooleanValue = () => Math.random() >= 0.5;
console.log(getRandomBooleanValue());
// false
console.log(getRandomBooleanValue());
// true
5. 查找平均数组值
const getAverageValue = (numbers) =>
numbers.reduce((total, currEl) => total + currEl, 0) / numbers.length;
console.log(getAverageValue([1, 2, 3, 4]));
// 2.5
console.log(getAverageValue([100, 900]));
// 500
6.检测暗模式
const darkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
7. 滚动到页面顶部
const scrollToTop =() => window.scrollTo({top: 0, behavior: 'smooth'});
8.查找字符串中指定字符的数量
const getCharAmount = (string, char) => string.split(char).length - 1;
console.log(getCharAmount("hello", "l"));
// 2
console.log(getCharAmount("hello", "q"));
// 0
9. 重定向网址
const redirectUrl = url => location.href = url;
redirectUrl("https://youtube.com");
10. 检测数组是否为空
const isArrayEmpty = (arr) => !Array.isArray(arr) || !arr.length;
console.log(isArrayEmpty([1, 2]));
// false
console.log(isArrayEmpty([]));
// true
11. 从数组返回唯一值
const getUniqueArrayValues = (arr) =>
arr.filter((i) => arr.indexOf(i) === arr.lastIndexOf(i));
console.log(getUniqueArrayValues([1, 2, 2, 3]));
// [1, 2, 3]
12. 从 DOM 获取选定的文本
const getSelectedText = () => window.getSelection().toString();
13. 检查表达式是否为数组
const isArray = (expression) => Array.isArray(expression);
console.log(isArray(1));
// false
console.log(isArray({ name: "Jaxongir" }));
// false
console.log(isArray(["JavaScript"]));
// false
14. Check a number if it's odd or even
const isOdd = (number) => number % 2 === 1;
console.log(isOdd(1));
// true
console.log(isOdd(2));
// false
15. Detect whether an element is in focus
const isElementFocussed = (element) => element === document.activeElement;
16. 将摄氏度转换为华氏度,反之亦然
const convertCelsiusToFahrenheit = (celsius) =>
Math.floor((celsius * 9) / 5 + 32);
const convertFahrenheitToCelsius = (fahrenheit) =>
Math.floor((fahrenheit - 32) * (5 / 9));
console.log(convertCelsiusToFahrenheit(21));
// 69
console.log(convertCelsiusToFahrenheit(-50));
// -58
console.log(convertFahrenheitToCelsius(33));
// 0
console.log(convertFahrenheitToCelsius(22));
// -6
17. 检查两个数组是否相同
const isArraysTheSame = (arr1, arr2) =>
arr1.length === arr2.length &&
arr1.every((val, index) => val === arr2[index]);
console.log(isArraysTheSame([1, 2], [1, 2]));
// true
console.log(isArraysTheSame([1, 2], [1, 3]));
// false
18.将第一个字符转换为大写
const convertFirstCharToUppercase = (str) =>
`${str[0].toUpperCase()}${str.slice(1)}`;
console.log(convertFirstCharToUppercase("jaxongir"));
// Jaxongir
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/292475.html