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.values(): ๊ฐ์ฒด์ value ๋ง ๊ฐ์ง๊ณ ์๋ ๋ฐฐ์ด๋ก ๋ฐํObject.keys(user).forEach( item => { console.log(user[item]) })
const user = { name: 'heropy', age: 85, isValid: true } Object.values(user) //['heropy', 85, false]
date method
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 })
- ๋ณ์์ ๋ด๋ ์๊ฐ์ ํธ์ถ๋ ์๊ฐ์ด ๋ด๊น
Date.now()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() // 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 ์ ๋นํด ๊ฐ๋ณ๋ค.(์ถ๊ฐ ํ๋ก๊ทธ์ธ์ด ํ์)
# ์ ๊ฐ์ฐ์ฐ์
: ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด์ ๊ดํธ๋ฅผ ๋ ๋ ค์ฃผ๋ ์ญํ
## ๋ฐฐ์ด์์
```js
const a =[1, 2, 3]
const b =[4, 5, 6]
const u = a.concat(b)
const c = [...a, ...b]
- ๋๋จธ์ง ์ฐ์ฐ์
const a =[1, 2, 3] function fn(z, ...rest){ console.log(z, rest) } fn(a) // 1, [2, 3]
๊ฐ์ฒด์์
const a1 = {
x: 1,
y: 2
}
const b1 = {
y: 3,
z: 4
}
const c1 = Object.assign({}, a1, b1)
const d = {...a1, ...b1}
console.log(c1)
console.log(d)
๊ตฌ์กฐ ๋ถํด ํ ๋น
- ๊ฐ์ฒด & ๋ฐฐ์ด์์ ํ์ฉ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถํด
- ์๋ ์์ฑ์ ๊บผ๋ด๋ฉด undefined
๊ธฐ๋ณธ๊ฐ ์ง์ ๋ณ์นญ ์ง์ nama ์ด๋ผ๋ ์์ฑ์ n ์ผ๋ก ์ฌ์ฉํ๊ฒ ๋ค.const user = { name: 'heropy', age: 85, isValid: true } const { email} = user //undefined
const { name : n} = user const n = user.name //๊ฐ์ ์๋ฏธ
const { email = 'sdkhdfk@gmail.com'} = user
- ์๋ ์์ฑ์ ๊บผ๋ด๋ฉด undefined
const user = { name: 'heropy', age: 85, isValid: true } const { isValid} = user //true
const { name: n, age: a, isValid: i} = user
const { name: n, ...rest} = user
//heropy {age: 85, isValid: true}
//rest๋ ๊ฐ์ฒด๋ฐ์ดํฐ๋ก
๋ฐฐ์ด ๊ตฌ์กฐ๋ถํด
๋ฐฐ์ด์ ์์๋๋ก ๊ตฌ์กฐ๋ถํด
const arr = [1, 2, 3]
const [x, y, z] = arr
const [,, z] = arr //3
๊ธฐ๋ณธ๊ฐ ์ง์
const arr = [1, 2, 3]
const [x, y, z, a = 99] = arr
console.log(a)//99
๋ค์๋ณด๊ธฐ
const user = {
name: 'heropy',
age: 85
}
for (const item of Object.entries(user)) {
console.log(item)
}
//['name', 'heropy']
//['age', 85]
for (const [k,v ] of Object.entries(user)) {
console.log(k, v)
}
//name heropy
//age 85
p์ w์ ๊ฐ์ ๋ฐ๊พธ๊ณ ์ถ์๋
let p =1
let w =3
;[p,w] =[w,p]
//; ๋ฅผ ์ฐ๋ ์ด์ ๋ ํํ์์ด ๋๋ฌ๋ค๋ ๊ฑธ ์๋ ค์ฃผ๊ธฐ ์ํด
์ ๊ฐ์ฐ์ฐ์๋ก ๋๋จธ์ง๋ฅผ ๋ฐ์ผ๋ฉด ๋ฐฐ์ด๋ก ๋ค์ด๊ฐ๋ค
const arr = [1,2,3,4,5,6,7,8]
const [x,y,...rest] = arr
console.log(x,y , rest)//1 2 [3, 4, 5, 6, 7, 8]
๋ฐ์ดํฐ ๊ฐ๋ณ์ฑ ๋ถ๋ณ์ฑ immutavility
๋ฐ์ํ
'๐ TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[220506]๐ข callstack / DOM api (0) | 2022.05.06 |
---|---|
[220430] ๋ฐ์ดํฐ์ ๋ถ๋ณ์ฑ๊ณผ ๊ฐ๋ณ์ฑ (0) | 2022.04.30 |
[220421] ์ฐ์ฐ์/if /switch (0) | 2022.04.22 |
[220419] export / import / lodash / Json (0) | 2022.04.19 |
[220418] JS data type (0) | 2022.04.18 |