πŸ’» 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 에 λΉ„ν•΄ 가볍닀.(μΆ”κ°€ ν”Œλ‘œκ·ΈμΈμ΄ ν•„μš”)
λ°˜μ‘ν˜•