[JS101] JavaScript - 常用的內建函式


Posted by yymarlerr on 2021-04-23

Number 類型的函式

Number(<>)

  • 將字串換成數值

parseInt(<字串>,<幾進位>)

  • 將字串換成數值

parseFloat(<字串>)

  • 把小數去掉 parseFloat(<字串>).toFixed(0)
  • 如果用parseFloat().toFixed(2) 則會保留小數兩位,以此類推
let number = '1.8888'
console.log(parseFloat(number).toFixed(1))

會得到 1.9

參考資料

Number.MAX_VALUE

  • 提醒可存最大數字為多少

參考 MDN

Math.PI

  • 約為 3.14159
  • PI 為 constant 常數

Math.ceil(<value>)

  • ceil 為天花板,往上取,無條件進位
  • console.log(Math.ceil(10.5)) => 11

Math.floor(<value>)

  • 無條件捨去

Math.round(<value>)

  • 四捨五入

Math.sqrt(<value>)

  • 開根號

Math.pow(<>, <>)

  • 次方
  • Math.pow(2, 10) => 1024
  • 也可以用 **,舉例:2 ** 3 => 8

Math.random()

  • 產生 0~1 的數字,有 0 沒有 1
  • 可以得到某個範圍的隨機數 console.log(Math.floor(Math.random()*10 + 1)) => 會得到 1~10 間的任意數字

abs(<數值>)

  • 絕對值

<value>.toString()

var a = 2
a.toString()
var a = 2
(a + '')

=> 變成字串的 2

String 類型的函式

<string>.toUpperCase()

<string>.toLowerCase()

<String>.charCodeAt(<位置>)

  • 可以得到英文字母所代表的號碼是多少
var a = 'BBAAAA'
var aCode = a.charCodeAt(2)
console.log(aCode)

=> 65 為 A 的 charCode
=> 97 為 a 的 CharCode
=> 98 為 b ⋯⋯ 以此類推

String.fromCharCode(<數值>)

  • 把數字變成英文字母
    Var str = String.fromCharCode(65)
    console.log(str)
    
    => A
  • 小寫轉大寫
    var a = 'g'
    var aCode = a.CharCodeAt(0)
    var str = String.fromCharCode(aCode - 32)
    console.log(str)
    
    => G

字串比大小

var char = 'g'
console.log(char >= 'a')

=> true

var char = 'J'
console.log(char >= 'A' && char <= 'Z'

=> true,所以 J 是大寫字母

<string>.indexOf(<stringtobefound>)

  • 會印出欲尋找字的首(index)個字母
  • 若所尋找的不存在,會回傳負數
    var str = 'hey hello world yoyyoyo'
    var index = str.indexOf('hello')
    console.log(index)
    
    => 4,代表在字串 str 所數到的第四個字母

replace(<>, <>)

  • 換掉某個字,只會換一個字
    var str = 'hey hello world' 
    str = str.replace('hey', '!!!')
    console.log(str)
    
    => !!! hello world yoyoyoyo

/<>/g

  • g 代表 global 的意思,表示每一個都會看過

split('<>')

  • 用什麼來切一串字
    var str = 'hey hello world yoyyoyo'
    console.log(str.split(' '))
    

trim()

  • 去掉空格
    var str = '    nananojot  '
    console.log(str.trim())
    

陣列相關函式

<陣列>.join('<>')

  • 用你想要的字元,將陣列的值接在一起,故頭尾不會有該字元
  • 預設值是空白,可將陣列變成字串
    var str = [30, 50, 65, 70]
    console.log(str.join('weight'))
    
    => 30weight50weight65weight70

<陣列>.map(<function>)

  • 把每一個元素帶到某個 function 裡面,並用回傳值取代原本的值
    *

    var score = [30, 50, 35, 20]
    function add(x){
      return x * 2
    }
    console.log(score.map(add))
    

    => [ 60, 100, 70, 40 ]

  • 將 function 放到 map 後面的寫法

    var score = [30, 50, 35, 20]
    console.log(score.map(function(x) {
      return x * 2
    }))
    
  • map 可無限接下去
    var score = [30, 50, 35, 20]
    console.log(score.map(function(x){
      return x * 2
    })
    .map(function add(x){
      return x * 3
    })
    )
    
    MDN

fliter(<function>)

  • 可以把東西給過濾掉,回傳 true 的東西會留下,回傳 false 的東西會不見
    var score = [30, 50, 35, -20]
    console.log(score.map(function(x) {
      return x * 2
    })
    .filter(function(x) {
     return x<0
    })
    )
    
  • 會回傳:[30, 50, 35]

    var score = [30, 50, 35, -20]
    console.log(score.filter(function(x) {
     return x>0
    })
    )
    
  • .filter().map() 後面的函式如果要放兩個參數的話,第二個參數會是第一個數值的 index`

slice(<>, <>,....)

  • 只想要陣列的某一個部分,
  • slice(2, 4) 第二個要,第四個不要,所以得到第二、第三個
  • 會回傳 ['nina']
    var name = ['seyah', 'nina', 'amber']
    console.log(name.slice(1, 2))
    
  • 不會改變原本的陣列,所以最後 console.log(name) 會回傳一樣的值
    var name = ['seyah', 'nina', 'amber']
    name.slice(1, 2)
    console.log(name)
    

splice(<start>, <delete>, <insert>...)

  • 會改變原本陣列,可同時刪除新增
  • 將 插入第 的前面
var name = ['seyah', 'nina', 'amber']
name.splice(1, 0, 'jasimine')
console.log(name)

<array>.sort(<function>)

  • 根據英文字母排列,會改變原本陣列
  • 如果排數字的話,會被當作字串去排,所以照第一個數字的大小去排
    var age = [1, 45, 8]
    age.sort()
    console.log(age)
    
    => [ 1, 45, 8 ]
  • 加入比大小的 function 功能,讓陣列照數字大小排列

    • 由大排到小,會回傳[ 8, 0, -45 ]
    • 想要換位置回傳 -1
    var age = [0, -45, 8]
    age.sort(function(a, b) {
        if(a === b) { 
        return 0 
        }
        if(a > b) { 
          return -1 
          }
        if(a < b) { 
          return 1 
          }
        }
    )
    console.log(age)
    
    • 由小排到大
    var age = [0, 45, 8]
    age.sort(function(a, b){
       return a - b
    })
    console.log(age)
    

MDN

.reduce(function(accumulator, currentValue) {return currentValue + accumulator }, initialValue}

  • 若有提供 initialValue,則 accumulator 為 intialValue,currentValue 為陣列的第一個元素
  • 或沒有提供 initialValue,則 accumulator 為陣列的第一個元素,currentValue為陣列的第二個元素
  • accumulator:函式 return 的值會是下一次的參數(accumulator)
  • 會回傳 192
    let score = [30, 50, 70, 40]
    function addUp(accumulator, currentValue) {return accumulator + currentValue}
    score = score.reduce(addUp, 2)
    console.log(score)
    

MDN










Related Posts

【JS上課筆記】JavaScript 的非同步請求:AJAX、Fetch API

【JS上課筆記】JavaScript 的非同步請求:AJAX、Fetch API

[Power BI] 讀書會 #3 Analysis Services 概念(2)

[Power BI] 讀書會 #3 Analysis Services 概念(2)

交換資料 - 表單、Ajax、XMLHttpRequest、CORS、JSONP

交換資料 - 表單、Ajax、XMLHttpRequest、CORS、JSONP


Comments