今天这篇文章重点介绍15个强大的单行代码处理技巧,可以解决很多常见的开发任务,现在,我们就开始今天的学习。
1、检查一个数字是否甚至是:
const isEven = n => n % 2 === 0
讯享网
示例:
讯享网console.log(isEven(4)); // trueconsole.log(isEven(5)); // false
描述:此技巧使用Modulo运算符(%)来确定n除以2的余数是0。如果是,则数为偶数;否则,这很奇怪。
2、从数组中获取最大值:
const max = arr => Math.max(…arr);
示例:
讯享网const numbers = [10, 5, 18];console.log(max(numbers)); // 18
描述:这使用扩展运算符(…')将数组arr扩展到Math.max()函数的单个参数中,该函数返回最大的值。</span></p><p style="margin-bottom: 16px;"><strong><span style="font-size: 16px;color: rgb(0, 166, 188);">3、从数组中获取最小值:</span></strong></p><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li></li></ul><pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">const</span> min = <span class="code-snippet__function"><span class="code-snippet__params">arr</span> =></span> <span class="code-snippet__built_in">Math</span>.min(...arr);</span></code></pre></section><p style="margin-bottom: 16px;"><span style="font-size: 16px;">示例:</span></p><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li></li><li></li></ul><pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">const</span> numbers = [<span class="code-snippet__number">10</span>, <span class="code-snippet__number">5</span>, <span class="code-snippet__number">18</span>];</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">console</span>.log(min(numbers)); <span class="code-snippet__comment">// 5</span></span></code></pre></section><p style="margin-bottom: 16px;"><span style="font-size: 16px;">描述:类似于最大值,此技巧使用Math.min()与传播操作员一起找到数组中的最小值。</span></p><p style="margin-bottom: 16px;"><strong><span style="font-size: 16px;color: rgb(0, 166, 188);">4、反向字符串:</span></strong><span style="font-size: 16px;"></span></p><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li></li></ul><pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer">const reverseString = str => str.split('').reverse().join('');</span></code></pre></section><p style="margin-bottom: 16px;"><span style="font-size: 16px;">示例:</span></p><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li></li><li></li></ul><pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer">const message = "Hello";</span></code><code><span class="code-snippet_outer">console.log(reverseString(message)); // "olleH"</span></code></pre></section><p style="margin-bottom: 16px;"><span style="font-size: 16px;">描述:这可以有效地使用一系列方法逆转字符串:</span></p><p style="margin-bottom: 16px;"><span style="font-size: 16px;">split(''):将字符串转换为字符数组。</span></p><p style="margin-bottom: 16px;"><span style="font-size: 16px;">reverse(): :逆转字符的顺序。</span></p><p style="margin-bottom: 16px;"><span style="font-size: 16px;">join(''):将反向字符重新连接到字符串中。</span></p><p style="margin-bottom: 16px;"><strong><span style="font-size: 16px;color: rgb(0, 166, 188);">5、检查字符串是否是回文:</span></strong><span style="font-size: 16px;"></span></p><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li></li></ul><pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">const</span> isPalindrome = <span class="code-snippet__function"><span class="code-snippet__params">str</span> =></span> str === str.split(<span class="code-snippet__string">''</span>).reverse().join(<span class="code-snippet__string">''</span>);</span></code></pre></section><p style="margin-bottom: 16px;"><span style="font-size: 16px;">示例:</span></p><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li></li><li></li></ul><pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer"><span class="code-snippet__built_in">console</span>.log(isPalindrome(<span class="code-snippet__string">"racecar"</span>)); <span class="code-snippet__comment">// true</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">console</span>.log(isPalindrome(<span class="code-snippet__string">"hello"</span>)); <span class="code-snippet__comment">// false</span></span></code></pre></section><p style="margin-bottom: 16px;"><span style="font-size: 16px;">描述:这建立在字符串反转技术上。它检查原始字符串是否与反向字符串相同,指示alendindrome。</span></p><p style="margin-bottom: 16px;"><strong><span style="font-size: 16px;color: rgb(0, 166, 188);">6、从数组中删除重复项:</span></strong><span style="font-size: 16px;"></span></p><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li></li></ul><pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">const</span> unique = <span class="code-snippet__function"><span class="code-snippet__params">arr</span> =></span> [...new <span class="code-snippet__built_in">Set</span>(arr)];</span></code></pre></section><p style="margin-bottom: 16px;"><span style="font-size: 16px;">示例:</span></p><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li></li><li></li></ul><pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer"><span class="code-snippet__keyword">const</span> numbers = [<span class="code-snippet__number">1</span>, <span class="code-snippet__number">2</span>, <span class="code-snippet__number">2</span>, <span class="code-snippet__number">3</span>, <span class="code-snippet__number">4</span>, <span class="code-snippet__number">1</span>];</span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">console</span>.log(unique(numbers)); <span class="code-snippet__comment">// [1, 2, 3, 4]</span></span></code></pre></section><p style="margin-bottom: 16px;"><span style="font-size: 16px;">描述:此单线使用集合来消除重复项。New Set(ARR)从数组中创建一个集合,固有地删除了重复项,而扩展运算符(…‘)将其转换回数组。
7、大写字符串的第一个字母:
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1);
示例:
讯享网const greeting = “hello”;console.log(capitalize(greeting)); // “Hello”
描述:这个单线大写了字符串的第一个字母。它提取第一个字符,将其转换为大写,并将其与字符串的其余部分相连。

8、检查字符串是否仅包含数字:
const isDigitsOnly = str => /^\d+\(/.test(str);</span></code></pre></section><p style="margin-bottom: 16px;"><span style="font-size: 16px;">示例:</span></p><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li></li><li></li></ul><pre class="code-snippet__js" data-lang="javascript"><code><span class="code-snippet_outer"><span class="code-snippet__built_in">console</span>.log(isDigitsOnly(<span class="code-snippet__string">"12345"</span>)); <span class="code-snippet__comment">// true</span></span></code><code><span class="code-snippet_outer"><span class="code-snippet__built_in">console</span>.log(isDigitsOnly(<span class="code-snippet__string">"hello"</span>)); <span class="code-snippet__comment">// false</span></span></code></pre></section><section style="margin-bottom: 16px;"><span style="font-size: 16px;">描述:这使用正则表达式检查字符串是否仅由数字组成。^\ d+\)匹配一个以一个或多个数字开始和结尾的字符串。
9、总和一个数组中的所有数字:
讯享网const sum = arr => arr.reduce((acc, val) => acc + val, 0);
示例:
const numbers = [1, 2, 3, 4];console.log(sum(numbers)); // 10
描述:这采用reald()方法通过数组迭代,将每个值添加到累加器,该值从0开始。
10、以yyyy-mm-dd格式获取当前日期:
讯享网const today = () => new Date().toISOString().split(’T’)[0];
示例:
console.log(today()); // e.g., “2024-10-12”
描述:这通过将当前日期转换为ISO字符串格式并提取日期零件来以标准化格式返回今天的日期。
11、生成一系列随机颜色:
讯享网const randomColors = n => Array.from({ length: n }, () => ’#’ + Math.floor(Math.random() * ).toString(16));
示例:
console.log(randomColors(3)); // e.g., [“#f0c”, “#a3d”, “#5c6”]
描述:这将使用narray.from()使用N随机十六进制颜色代码创建一个数组,并用随机数生成每种颜色。
12、洗牌阵列:
讯享网const shuffle = arr => arr.sort(() => Math.random() - 0.5);
示例:
console.log(shuffle([1, 2, 3])); // e.g., [3, 1, 2]
描述:这通过基于随机比较器函数对数组中的元素顺序随机。
13、计算数字的阶乘:
讯享网const factorial = n => n <= 1 ? 1 : n * factorial(n - 1);
示例:
console.log(factorial(5)); // 120
描述:这将计算所有正整数的乘积,最多递归n。
14、检查对象是否为空:
讯享网const isEmpty = obj => Object.keys(obj).length === 0;
示例:

console.log(isEmpty({})); // true
描述:这可以通过验证其键数组的长度来检查对象是否没有属性。
15、将骆驼字符串转换为连接式:
讯享网const camelToKebab = str => str.replace(/([a-z])([A-Z])/g, ‘\(1-\)2’).toLowerCase();
示例:
const camelCaseString = “thisIsCamelCase”;console.log(camelToKebab(camelCaseString)); // “this-is-camel-case”
描述:此将骆驼式字符串转换为连接式案例,使用正则表达式:
替换(/([A-Z])([[A-Z])/g,’\( 1- \) 2’):查找小写字母的出现,然后是大写字母,并用小写字母,连字符和大写字母代替。
tolowercase():将整个结果的字符串转换为小写,以进行适当的烤肉串格式。
这些单行对于在JavaScript中快速操作很有用,可以帮助你写出更加简洁的代码。
最后,希望今天的内容对你有所帮助,感谢你的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/187913.html