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 ์ ๋นํด ๊ฐ๋ณ๋ค.(์ถ๊ฐ ํ๋ก๊ทธ์ธ์ด ํ์)
๋ฐ์ํ
'๐ป dev > ๐ฉ๐ปโ๐ป ํํ ํฌ ํ๋ก ํธ์๋ ๊ฐ๋ฐ์ ๊ณผ์ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SCSS] ๊ธฐ๋ณธ ๋ฌธ๋ฒ ๋ฐ ์ฌ์ฉ ๋ฐฉ๋ฒ (0) | 2022.05.09 |
---|---|
[Project] css ํด๋ก ์ฝ๋ฉ (0) | 2022.04.29 |
[220419] JS function (1) | 2022.04.19 |
[220413] npm / packae.json /parcel (0) | 2022.04.13 |
[Node.js] nvm ์ค์น (0) | 2022.04.13 |