<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          28 個常見的JavaScript開發(fā)技巧

          共 4973字,需瀏覽 10分鐘

           ·

          2021-10-26 10:39

          英文 | https://niemvuilaptrinh.medium.com/28-tip-javascript-you-should-know-5c8ca83e4f99

          翻譯 | 楊小愛


          今天我將介紹一些Javascript中的常用技巧,以幫助您解決問題。
          以下是在開發(fā)過程中的一些常見問題,希望能夠幫助你提升開發(fā)效率。
          1、 Javascript 反向字符串

          以下是代碼:

          /*niemvuilaptrinh.com*/const stringReverse = str => str.split("").reverse().join("");stringReverse('hello world'); /*dlrow olleh*/

          2、滾動到頁面頂部

          以下是代碼:

          /*niemvuilaptrinh.com*/const scrollToTop = () => window.scrollTo(0, 0);scrollToTop();

          3、刪除數(shù)組中的重復項

          以下是代碼:
          /*niemvuilaptrinh.com*/const removeDuplicate = (arr) => [...new Set(arr)];removeDuplicate([1, 2, 3, 4, 4, 2, 1]); // [1, 2, 3, 4]

          4、獲取數(shù)組中的隨機項

          以下是代碼:
          /*niemvuilaptrinh.com*/const randomItemArray = (arr) => arr[Math.floor(Math.random() * arr.length)];randomItemArray(['a', 'b', 'c', 1, 2, 3]);

          5、獲取數(shù)組中的最大數(shù)

          以下是代碼:
          /*niemvuilaptrinh.com*/const maxNumber = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);maxNumber([4,9,5,7,2]) /* 9 */

          6、 檢查型號

          以下是代碼:

          /*niemvuilaptrinh.com*/function isNumber(num) {  return !isNaN(parseFloat(num)) && isFinite(num);}isNumber("Hello"); /*false*/isNumber(123);/*true*/

          7、檢查類型為空

          以下是代碼:
          /*niemvuilaptrinh.com*/const checkNull = val => val === undefined || val === null;checkNull(123) /* false */checkNull() /* true */checkNull('hello') /* false */

          8、獲取數(shù)組中的最小數(shù)

          以下是代碼:
          /*niemvuilaptrinh.com*/const minNumber = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);console.log(minNumber([3,5,9,7,1])) /*1*/

          9、 獲取數(shù)組中的平均數(shù)

          以下是代碼:
          /*niemvuilaptrinh.com*/const averageNumber = arr => arr.reduce((a, b) => a + b) / arr.length;averageNumber([1, 2, 3, 4, 5]) /* 3 */

          10、檢查元素的類型

          以下是代碼:
          /*niemvuilaptrinh.com*//*niemvuilaptrinh.com*/ const checkType = v => v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();checkType(true) /*boolean*/checkType("hello World") /*string*/checkType(123) /*number*/

          11、計算數(shù)組中元素的出現(xiàn)次數(shù)

          以下是代碼:
          /*niemvuilaptrinh.com*/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 */

          12、使用 Javascript 獲取當前 URL

          以下是代碼:
          /*niemvuilaptrinh.com*/const getCurrentURL = () => window.location.href;getCurrentURL() /* https://www.niemvuilaptrinh.com */

          13、大寫字符串中的字母

          以下是代碼:

          /*niemvuilaptrinh.com*/const capitalizeString = str => str.replace(/b[a-z]/g, char => char.toUpperCase());capitalizeString('niem vui lap trinh'); /* 'Niem Vui Lap Trinh' */

          14、將 RGB 轉(zhuǎn)換為十六進制

          以下是代碼:
          /*niemvuilaptrinh.com*/ 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'*/

          15、將數(shù)字轉(zhuǎn)換為數(shù)組

          以下是代碼:
          /*niemvuilaptrinh.com*/const numberToArray = n => [...`${n}`].map(i => parseInt(i));numberToArray(246) /*[2, 4, 6]*/numberToArray(357911) /*[3, 5, 7, 9, 1, 1]*/

          16、從 HTML 中獲取內(nèi)容

          以下是代碼:
          /*niemvuilaptrinh.com*/const getTextInHTML = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || '';getTextInHTML('<h2>Hello World</h2>'); /*'Hello World'*/

          17、 在 JS 中分配多個變量

          以下是代碼:
          /*niemvuilaptrinh.com*/var [a,b,c,d] = [1, 2, 'Hello', false];console.log(a,b,c,d) /* 1 2 'Hello' false */

          18、 空數(shù)組

          以下是代碼:
          let arr = [1, 2, 3, 4, 5];arr.length = 0;console.log(arr); /* K?t qu? : [] */

          19、 在 JS 中復制對象

          以下是代碼:
          /*niemvuilaptrinh.com*/const obj = {    name: "niem vui lap trinh",    age: 12};const copyObject = { ...obj };console.log(copyObject); /* {name: 'niem vui lap trinh', age: 12}*/

          20、檢查偶數(shù)和奇數(shù)

          以下是代碼:
          /*niemvuilaptrinh.com*/const isEven = num => num % 2 === 0;console.log(isEven(1)); /*false*/console.log(isEven(2)); /*true*/

          21、 合并兩個或多個數(shù)組 JS

          以下是代碼:
          /*niemvuilaptrinh.com*/const arr1 = [1, 2, 3];const arr2 = [4, 5, 6];const arr = arr1.concat(arr2);console.log(arr); /* [1, 2, 3, 4, 5, 6] */

          22、 將內(nèi)容復制到剪貼板

          以下是代碼:
          /*niemvuilaptrinh.com*/const copyTextToClipboard = async (text) => {  await navigator.clipboard.writeText(text)}

          23、從一系列值中選擇一個隨機數(shù)

          以下是代碼:
          /*niemvuilaptrinh.com*/var max = 10;var min = 1;var numRandom = Math.floor(Math.random() * (max - min + 1)) + min;console.log(numRandom)

          24、檢查元素是否聚焦

          以下是代碼:
          /*niemvuilaptrinh.com*/const elementFocus = (el) => (el === document.activeElement);elementIsInFocus(element);/*if true element is focus*//*if false element is not focus*/

          25、用 JS 測試蘋果設備

          以下是代碼:
          /*niemvuilaptrinh.com*/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*/

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

          以下是代碼:
          /*niemvuilaptrinh.com*/const str = "Hello";const arr = [...str];console.log(arr); /* ['H', 'e', 'l', 'l', 'o'] *

          27、在 JS 中使用箭頭函數(shù)

          以下是代碼:
          /* regular function*/const sum = function(x, y) {  return x + y;};/* arrow function */const sum = (x, y) => x + y;

          28、 條件句的簡寫

          總結(jié)
          我希望這篇文章能為您提供對開發(fā)網(wǎng)站有用的 javascript 知識,如果您有任何問題,請在留言區(qū)給我留言。 
          感謝您的支持,也感謝您的閱讀,如果您覺得今天的內(nèi)容對您有幫助,請記得給我點個贊。


          學習更多技能

          請點擊下方公眾號

          瀏覽 64
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚州精品无码视频 | 日本三级电影久久久影院 | 18禁www网站 | 香蕉电影伊人 | 免费黄片在线看 |