<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 個(gè)你應(yīng)該知道的JS 實(shí)用小技巧

          共 4921字,需瀏覽 10分鐘

           ·

          2022-01-10 14:35


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


          今天我將分享一些Javascript中的常用技巧,以幫助您解決問(wèn)題。設(shè)置過(guò)程中的常見(jiàn)問(wèn)題更快更容易。
          01、Javascript 反向字符串

          下面是代碼:
          /*niemvuilaptrinh.com*/const stringReverse = str => str.split("").reverse().join("");stringReverse('hello world'); /*dlrow olleh*/
          02、滾動(dòng)到頁(yè)面頂部

          下面是代碼:
          /*niemvuilaptrinh.com*/const scrollToTop = () => window.scrollTo(0, 0);scrollToTop();
          03、刪除數(shù)組中的重復(fù)項(xiàng)

          下面是代碼:
          /*niemvuilaptrinh.com*/const removeDuplicate = (arr) => [...new Set(arr)];removeDuplicate([1, 2, 3, 4, 4, 2, 1]); // [1, 2, 3, 4]
          04、 獲取數(shù)組中的隨機(jī)項(xiàng)

          下面是代碼:
          /*niemvuilaptrinh.com*/const randomItemArray = (arr) => arr[Math.floor(Math.random() * arr.length)];randomItemArray(['a', 'b', 'c', 1, 2, 3]);
          05、獲取數(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 */
          06、檢查型號(hào)

          下面是代碼:
          /*niemvuilaptrinh.com*/function isNumber(num) {  return !isNaN(parseFloat(num)) && isFinite(num);}isNumber("Hello"); /*false*/isNumber(123);/*true*/
          07、檢查類(lèi)型為空

          下面是代碼:
          /*niemvuilaptrinh.com*/const checkNull = val => val === undefined || val === null;checkNull(123) /* false */checkNull() /* true */checkNull('hello') /* false */
          08、獲取數(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*/
          09、獲取數(shù)組中的平均數(shù)

          下面是代碼:
          /*niemvuilaptrinh.com*/const averageNumber = arr => arr.reduce((a, b) => a + b) / arr.length;averageNumber([1, 2, 3, 4, 5]) /* 3 */
          10、檢查元素的類(lèi)型

          下面是代碼:
          /*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、 計(jì)算數(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 獲取當(dāng)前 URL

          下面是代碼:
          /*niemvuilaptrinh.com*/const getCurrentURL = () => window.location.href;getCurrentURL() /* https://www.niemvuilaptrinh.com */
          13、大寫(xiě)字符串中的字母

          下面是代碼:
          /*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)換為十六進(jì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 中分配多個(gè)變量

          下面是代碼:
          /*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 中復(fù)制對(duì)象

          下面是代碼:
          /*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、合并兩個(gè)或多個(gè)數(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)容復(fù)制到剪貼板

          下面是代碼:
          /*niemvuilaptrinh.com*/const copyTextToClipboard = async (text) => {  await navigator.clipboard.writeText(text)}
          23、從一系列值中選擇一個(gè)隨機(jī)數(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 測(cè)試蘋(píng)果設(shè)備

          下面是代碼:
          /*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ǎn)寫(xiě)

          總結(jié):
          我希望這篇文章能為您提供對(duì)開(kāi)發(fā)網(wǎng)站有用的javascript知識(shí),如果您有任何問(wèn)題,請(qǐng)留言區(qū)給我留言,我會(huì)盡快回復(fù)。
          感謝您的閱讀,祝您今天過(guò)得愉快!



          學(xué)習(xí)更多技能

          請(qǐng)點(diǎn)擊下方公眾號(hào)

          瀏覽 62
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  成人免费无码大片A毛片 | 插逼免费| 欧洲中文字幕日韩精品成人 | 国内无码精品 | 免费一级做a爱片毛片A片小说 |