π» dev/π©π»π» νν
ν¬ νλ‘ νΈμλ κ°λ°μ κ³Όμ
[JS] νμ€ λ΄μ₯ κ°μ²΄ (μ λ°μ΄νΈ μμ )
k_m_jin
2022. 4. 25. 23:53
arrray method
.reduce()
const arr = [1,2,3,4]
let sum =0
for (let i = 0; i < arr.length; i += 1){
sum += arr[i]
}
console.log(sum)
const arr = [1, 2, 3, 4]
arr.reduce((acc, cur) => acc + cur, 0)
const data = [
'regNum',
'name',
'logo',
'representative',
'startDate',
'address',
'contactEmail',
'tags',
'intro',
'homepage',
'introFile'
]
const res = data.reduce((acc, name) => Object.assign(acc, {
[name] () {
console.log(123)
}
}), {})
.reverse()
: μμμ μμ λ°μ
const arr = [1,2,3,4,5]
console.log(arr.reverse())//[5, 4, 3, 2, 1]
console.log(arr)//[5, 4, 3, 2, 1]
.slice()
: μμμΈλ±μ€λΆν° μ’ λ£μΈλ±μ€μ μ§μ κΉμ§ μλΌμ μλ‘μ΄ λ°°μ΄λ‘ λ°ν
const arr = [1,2,3,4,5]
arr.slice(0,3) //[1, 2, 3]
.some( )
: μμ μ€ νκ°λΌλ νλ³ν¨μλ₯Ό ν΅κ³Όνλ©΄ true, λΉ λ°°μ΄μμ νΈμΆνλ©΄ 무쑰건 false λ₯Ό λ°ν
const arr = [1,2,3,4,5]
console.log(
arr.some(item => item === 1)
)//true
.splice( λμμμμΈλ±μ€, [κ°―μ], [μΆκ°λ°μ΄ν°])
: λ°°μ΄μ κΈ°μ‘΄ μμλ₯Ό λ³κ²½ν΄μ λ³κ²½λ λ°°μ΄μ λ°ν
const arr = ['a', 'b', 'c', 'd']
console.log(
arr.splice(2,2, 'x')
)//['c', 'd']
console.log(arr)//['a', 'b', 'x']
μΆκ°λ§ ν κ²½μ°
const arr = ['a', 'b', 'c', 'd']
// .splice(μΈλ±μ€, μμ κ°μ, μΆκ°λ°μ΄ν°)
console.log(
arr.splice(2,0, 'x')
)//[]
console.log(arr)//['a', 'b', 'x', 'c', 'd']
object method
Object.assign(λμ, μΆμ², ..)
: λμμ μΆμ²λ€μ 볡λΆ
μλ³Έ μμ
const user = {
name: 'heropy',
age: 85,
isValid: true
}
const userB = {}
Object.assign(λμ, μΆμ²) // λμμμΈ μΆμ²λ€λ₯Ό 볡λΆ
// μλ‘μ΄ λ°°μ΄μ μμ±νλ λ°©λ²
Object.assign({}, user, userB)
Object.entries(κ°μ²΄)
: κ°μ²΄λ₯Ό 2μ°¨μ λ°°μ΄λ‘ λ°ν. κ° keyμ valueλ₯Ό [key, value]
const user = {
name: 'heropy',
age: 85,
isValid: true
}
Object.entries(user)
// κ²°κ³Ό
// [
// ["name","heropy"],
// ["age", 85],
// ["isValid",true]
// ]
for(const item of Object.entries(user) ) {
console.log(item[0])//key
console.log(item[1])//value
}
Object.keys(κ°μ²΄)
: κ°μ²΄μ keyλ€μ λ°°μ΄λ‘ λ°ν
const user = {
name: 'heropy',
age: 85,
isValid: true
}
Object.keys(user) //['name', 'age', 'isValid']
- κ° μμ±λ€μ κΊΌλΌ λ
Object.keys(user).forEach( item => { console.log(user[item]) })
λ€μ보기
const state = { name: '', age: '', isValid: false } const mutations = { setState(payload) { Object.keys(payload).forEach(key => { state[key] = payload[key] }) } } mutations.setState({ name: 'Heropy', age: 85 })
Object.values()
: κ°μ²΄μ value λ§ κ°μ§κ³ μλ λ°°μ΄λ‘ λ°νconst user = { name: 'heropy', age: 85, isValid: true } Object.values(user) //['heropy', 85, false]
date method
- λ³μμ λ΄λ μκ°μ νΈμΆλ μκ°μ΄ λ΄κΉ
const date = new Date() date.getFullYear() // νμ¬λ λ date.getMonth() // zero based 1μμ΄ 0 date.getDate() date.getDay() // μΌμμΌμ΄ 0 date.getHours() date.getMinutes() date.getSeconds() new Date().getSeconds()
Date.now()
Date.now() // 1970.01.01 00:00:00 λ‘ λΆν° νμ¬κΉμ§μ μκ°μ ms λ‘ νν
- μμ μκ° κ³μ°
const now = Date.now()
for (let i =0; i< 1000; i += 1) {
console.log('')
}
console.log(Date.now()- now)
//78
> moment js : λ μ§ ν¬λ§·μ μ½κ² λ§λ€ μ μλ€.
> day js : moment μ λΉν΄ κ°λ³λ€.(μΆκ° νλ‘κ·ΈμΈμ΄ νμ)
λ°μν