21 必须有 JavaScript 一衬里
2022/11/2 1:24:53
本文主要是介绍21 必须有 JavaScript 一衬里,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
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
这篇关于21 必须有 JavaScript 一衬里的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-22[开源]10.3K+ Star!轻量强大的开源运维平台,超赞!
- 2024-11-21Flutter基础教程:新手入门指南
- 2024-11-21Flutter跨平台教程:新手入门详解
- 2024-11-21Flutter跨平台教程:新手入门与实践指南
- 2024-11-21Flutter列表组件教程:初学者指南
- 2024-11-21Flutter列表组件教程:新手入门指南
- 2024-11-21Flutter入门教程:初学者必看指南
- 2024-11-21Flutter入门教程:从零开始的Flutter开发指南
- 2024-11-21Flutter升级教程:新手必读的升级指南
- 2024-11-21Flutter升级教程:轻松掌握Flutter版本更新