自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

11個(gè) 殺手級(jí) JavaScript 單行代碼

開發(fā) 前端
每個(gè) JS 開發(fā)人員都應(yīng)該使用 javascript one liner 來提高生產(chǎn)力和技能,所以今天我們討論一些可以在日常開發(fā)生活中使用的 one liner。

每個(gè) JS 開發(fā)人員都應(yīng)該使用 javascript one liner 來提高生產(chǎn)力和技能,所以今天我們討論一些可以在日常開發(fā)生活中使用的 one liner。

1. 對(duì)數(shù)組進(jìn)行排序

使用 sort 方法對(duì)數(shù)組進(jìn)行排序非常簡(jiǎn)單。

const number = [2,6,3,7,8,4,0];number.sort();// expected output: [0,2,3,4,6,7,8]

2.檢查數(shù)組中的值

很多時(shí)候我們需要檢查值是否存在于數(shù)組中,借助 include 方法。

const array1 = [1, 2, 3];console.log(array1.includes(2));// expected output: true

3.過濾數(shù)組

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];const result = words.filter(word word.length > 6);console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

4. 從數(shù)組中查找元素

如果你只需要一個(gè)元素,但你在數(shù)組中獲得了很多元素,不要擔(dān)心 JavaScript 有 find 方法。

const array1 = [5, 12, 8, 130, 44];const found = array1.find(element element > 10);console.log(found);// expected output: 12

5. 查找數(shù)組中任何元素的索引

要查找數(shù)組中元素的索引,您可以簡(jiǎn)單地使用 indexOf 方法。

const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];console.log(beasts.indexOf('bison'));// expected output: 1

6. 將數(shù)組轉(zhuǎn)換為字符串

const elements = ['Fire', 'Air', 'Water'];console.log(elements.join(", "));// expected output: "Fire, Air, Water"

7.支票號(hào)碼是偶數(shù)還是奇數(shù)

很容易找出給定的數(shù)字是偶數(shù)還是奇數(shù)。

const isEven = num num % 2 === 0;orconst isEven = num !(n & 1);

8.刪除數(shù)組中的所有重復(fù)值

刪除數(shù)組中所有重復(fù)值的一種非常簡(jiǎn)單的方法

const setArray = arr [...new Set(arr)];const arr = [1,2,3,4,5,1,3,4,5,2,6];setArray(arr);// expected output: [1,2,3,4,5,6]

9. 合并多個(gè)數(shù)組的不同方式

// merge but don't remove duplicationsconst merge = (a, b) =>orconst merge = (a, b) => [...a, ...b];// merge with remove duplicationsconst merge = (a, b) => [...new Set(a.concat(b))];orconst merge = (a, b) => [...new Set([...a, ...b])];

10. 滾動(dòng)到頁面頂部

有很多方法可以將頁面滾動(dòng)到頂部。

const goToTop = () window.scrollTo(0,0, "smooth");orconst scrollToTop = (element) => element.scrollIntoView({behavior: "smooth", block: "start"});// scroll to bottom of the pageconst scrollToBottom = () window.scrollTo(0, document.body.scrollHeight);

11.復(fù)制到剪貼板

在 Web 應(yīng)用程序中,復(fù)制到剪貼板因其對(duì)用戶的便利性而迅速普及。

const copyToClipboard = text (navigator.clipboard?.writeText ?? Promise.reject)(text);

寫在最后

以上就是我今天跟你分享的11個(gè)JavaScript的單行代碼技巧,希望你能從中學(xué)到新的知識(shí)。

責(zé)任編輯:華軒 來源: web前端開發(fā)
相關(guān)推薦

2023-06-14 15:51:48

JavaScript

2022-11-28 23:44:26

JavaScript技巧程序員

2022-09-26 12:53:54

JavaScrip單行代碼

2025-02-18 11:01:49

2023-02-15 16:19:59

JavaScript技巧API

2022-12-19 15:23:51

JavaScrip開發(fā)語言

2023-05-30 15:11:16

JavaScrip開發(fā)功能

2023-10-10 16:20:38

JavaScript代碼技巧

2024-10-09 14:45:41

2023-03-13 16:08:00

JavaScript數(shù)組函數(shù)

2023-08-27 16:19:09

JavaScript編程語言

2022-07-12 10:18:05

JavaScript單行代碼

2022-07-08 09:53:51

JavaScript單行代碼

2022-09-02 23:08:04

JavaScript技巧開發(fā)

2025-02-25 11:12:53

2025-02-19 10:35:57

2022-07-06 08:39:33

Python代碼

2022-10-09 18:52:11

JavaScript開發(fā)數(shù)組

2022-10-08 07:54:24

JavaScriptAPI代碼

2024-09-04 14:00:16

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)