Latest commit

History

History
514 lines (329 loc) ยท 12.2 KB

File metadata and controls

514 lines (329 loc) ยท 12.2 KB

๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด

๋ฐฐ์—ด(Arrays)

๋ฐฐ์—ด์˜ ์ •์˜์™€ ํŠน์ง•

  • ํ‚ค์™€ ์†์„ฑ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฐ์ฒด(object)
  • ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” ํŠน์ง•์ด ์žˆ์Œ
  • ์ฃผ๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ , 0์„ ํฌํ•จํ•œ ์–‘์˜ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ํŠน์ • ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” array.length ํ˜•ํƒœ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋Š” array.length - 1 ๋กœ ์ ‘๊ทผ

image-20211106151517307

I. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (1) - ๊ธฐ๋ณธํŽธ (๊ธฐ๋ณธ ๋ฐฐ์—ด ์กฐ์ž‘)

image-20211106151555718

I - 1. reverse

  • array.reverse()
    • ์›๋ณธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ

image-20211106151649779

I - 2. push & pop

  • array.push()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
  • array.pop()
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152033152

I - 3. unshift & shift

  • array.unshift()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
  • array.shift()
    • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152115109

I - 4. includes

  • array.includes(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋ณ„ ํ›„ ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106152159364

I - 5. indexOf

  • array.indexOf(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ํ›„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ๋กœ ์ฐพ์€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
    • ๋งŒ์•ฝ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ -1 ๋ฐ˜ํ™˜

image-20211106152620202

I - 6. join

  • array.join([separator])
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜
    • separator(๊ตฌ๋ถ„์ž)๋Š” ์„ ํƒ์ ์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ƒ๋žต ์‹œ ์‰ผํ‘œ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ

image-20211106152753676

์‹ค์Šต

/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 1]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ null ๊ฐ’์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.*/consthomeworks=['david.zip',null,'maria.zip','tom.zip',null]constresult=[]for(lethomeworkofhomeworks){if(homework){result.push(homework)}}console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 2]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜ ๋ฌธ์ž์—ด์„ ์™„์„ฑํ•˜์„ธ์š”.	'www.samsung.com/sec/buds/galaxy-buds-pro'*/constarr1=['www','samsung','com']constarr2=['galaxy','buds','pro']constarr3=['sec','buds']constmainURL=arr1.join('.')constmodel=arr2.join('-')arr3.unshift(mainURL)arr3.push(model)constresult=arr3.join('/')console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 3]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ๋ชจ๋“  'rainy' ์š”์†Œ๋ฅผ 'sunny'๋กœ ๊ต์ฒดํ•˜์„ธ์š”	- indexOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.*/constweather=['sunny','sunny','sunny','sunny','rainy','rainy','sunny']for(lettempofweather){if(temp=='rainy'){letidx=weather.indexOf('rainy')weather[idx]='sunny'}}letidx=weather.indexOf('rainy')while(idx>0){weather[idx]='sunny'idx=weather.indexOf('rainy')}console.log(weather)

II. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (2) - ์‹ฌํ™”ํŽธ (Array Helper Methods)

  • ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํŠน์ • ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ธ์ž๋กœ callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด ํŠน์ง•

image-20211106153029911

II - 1. forEach

  • array.forEach(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” 3๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ
      • element: ๋ฐฐ์—ด์˜ ์š”์†Œ
      • index: ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค
      • array: ๋ฐฐ์—ด ์ž์ฒด
    • ๋ฐ˜ํ™˜ ๊ฐ’(return)์ด ์—†๋Š” ๋ฉ”์„œ๋“œ

image-20211106153701098

II - 2. map

  • array.map(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์š”์†Œ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉ

image-20211106153757715

II - 3. filter

  • array.filter(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ธ ์š”์†Œ๋“ค๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋งํ•  ๋•Œ ์œ ์šฉ

image-20211106153900239

II - 4. reduce

  • array.reduce(callback(acc, element, [index[, array]])[, intialValue])

image-20211106154012229

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๊ฐ’(acc)์— ๋ˆ„์  ํ›„ ๋ฐ˜ํ™˜
  • reduce ๋ฉ”์„œ๋“œ์˜ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜
    • acc
      • ์ด์ „ callback ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ๋ˆ„์ ๋˜๋Š” ๋ณ€์ˆ˜
    • initialValue(optional)
      • ์ตœ์ดˆ callback ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ acc์— ํ• ๋‹น ๋˜๋Š” ๊ฐ’, default ๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’

์˜ˆ์‹œ

image-20211106154132959

image-20211106154155526

II - 5. find

  • array.find(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ด๋ฉด ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    • ์ฐพ๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์— ์—†์œผ๋ฉด undefined ๋ฐ˜ํ™˜

image-20211106154318481

II - 6. some

  • array.some(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106154411525

II - 7. every

  • array.every(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ์ฐธ ๋ฐ˜ํ™˜

image-20211106154458758

์‹ค์Šต

/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 1] ์†๋ ฅ(distance/time)์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด speeds๋ฅผ ๋งŒ๋“œ์„ธ์š”.*/consttrips=[{distance: 34,time: 10},{distance: 90,time: 50},{distance: 59,time: 25},]constspeeds=trips.map((trip)=>{returntrip.distance/trip.time})console.log(speeds)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 2] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํŠน์ • ๋ฌธ์ž(query)๊ฐ€ ํฌํ•จ๋˜๋Š” ์š”์†Œ๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.*/constlanguages=['python','javascript','html','java']constquery='java'constresults=languages.filter((language)=>{returnlanguage.includes(query)})console.log(results)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 3] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ์„ธ์š”. { smith: 90, peter: 80, anna: 85, }*/constscores=[{name: 'smith',score: 90},{name: 'peter',score: 80},{name: 'anna',score: 85},]constresult=scores.reduce((res,score)=>{res[score.name]=score.scorereturnres},{})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 4] ์ฃผ์–ด์ง„ accounts ๋ฐฐ์—ด์—์„œ balance๊ฐ€ 24,000์ธ ์‚ฌ๋žŒ์„ ์ฐพ์œผ์„ธ์š”.*/constaccounts=[{name: 'justin',balance: 1200},{name: 'harry',balance: 50000},{name: 'jason',balance: 24000},]constresult=accounts.find((account)=>{returnaccount.balance===24000})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 5] ์ฃผ์–ด์ง„ requests ๋ฐฐ์—ด์—์„œ status๊ฐ€ pending์ธ ์š”์ฒญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.*/constrequests=[{url: '/photos',status: 'complete'},{url: '/albums',status: 'pending'},{url: '/users',status: 'failed'},]constresult=requests.some((req)=>{returnreq.status==='pending'})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 6] ์ฃผ์–ด์ง„ users ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ชจ๋“  ์œ ์ €์˜ ์ƒํƒœ๊ฐ€ submmited์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์„ธ์š”.*/constusers=[{username: 'eric',submitted: true},{username: 'justin',submitted: true},{username: 'james',submitted: false},]constresult=users.every((user)=>{returnuser.submitted})console.log(result)constkey='regions'constvalue=['๊ตฌ๋ฏธ','๊ด‘์ฃผ','๋Œ€์ „','์„œ์šธ','๋ถ€์šธ๊ฒฝ']constloca={[key]: value,}console.log(loca)

๊ฐ์ฒด (Objects)

๊ฐ์ฒด์˜ ์ •์˜์™€ ํŠน์ง•

  • ๊ฐ์ฒด๋Š” ์†์„ฑ(property)์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€์— key์™€ value์˜ ์Œ์œผ๋กœ ํ‘œํ˜„
  • key๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ
  • value๋Š” ๋ชจ๋“  ํƒ€์ž… ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด ์š”์†Œ ์ ‘๊ทผ์€ ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ€๋Šฅ
    • key ์ด๋ฆ„์— ๋„์–ด์“ฐ๊ธฐ ๊ฐ™์€ ๊ตฌ๋ถ„์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋Œ€๊ด€ํ˜ธ ์ ‘๊ทผ๋งŒ ๊ฐ€๋Šฅ

I. ๊ฐ์ฒด ๊ด€๋ จ ES6 ๋ฌธ๋ฒ•

I - 1. ์†์„ฑ๋ช… ์ถ•์•ฝ (shorthand)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์™€ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถ•์•ฝ ๊ฐ€๋Šฅ

image-20211106154839156

I - 2. ๋ฉ”์„œ๋“œ๋ช… ์ถ•์•ฝ(shorthand)

  • ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ function ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ

image-20211106160105850

I - 3. ๊ณ„์‚ฐ๋œ ์†์„ฑ(computed property name)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์˜ ์ด๋ฆ„์„ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ

image-20211106160915728

I - 4. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (destructing assignment)

  • ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์†์„ฑ์„ ๋ณ€์ˆ˜์— ์‰ฝ๊ฒŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•

image-20211106161339931

์‹ค์Šต

/* [Object ์š”์†Œ ์ ‘๊ทผ ์—ฐ์Šต]	์ฃผ์–ด์ง„ ๊ฐ์ฒด์—์„œ items์˜ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ์˜ name์„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์„ธ์š”.*/constdata={id: 42,items: [{id: 1,name: 'foo',},{id: 2,name: 'bar',},],}constresult=data.items[0].nameconsole.log(result)/*[Object ์ถ•์•ฝ ๋ฌธ๋ฒ•]์•„๋ž˜ ๋ณ€์ˆ˜๋“ค์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๋Š” Object๋ฅผ ์ถ•์•ฝ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.*/constusername='hailey'constcontact='010-1234-5678'constresult={
username,
contact
}console.log(result)/*[Object Destructuring]์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ Object ์ถ•์•ฝ ๋ฌธ๋ฒ•๊ณผ, destructuring์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ž‘์„ฑํ•˜์„ธ์š”.*/constusers=[{name: 'hailey',contact: '010-1234-5678',},{name: 'paul',contact: '010-5678-1234',},]// ๋ณ€๊ฒฝ ํ›„functionsaveUserData(users){// Destructuring// const { name, contact } = userconstuserData=users.map(({ name, contact },id)=>{// ์ถ•์•ฝ ๋ฌธ๋ฒ• ํ‚ค์™€ ๋ฒจ๋ฅ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ํ•˜๋‚˜๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ!return{id, name, contact}})returnuserData}// ๋ณ€๊ฒฝ ์ „functionsaveUserData(users){constuserData=users.map((user,index)=>{return{id: index,name: user.name,contact: user.contact}})returnuserData}

II. JSON (JavaScript Object Notation)

  • key-value์Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ์–ธ์–ด ๋…๋ฆฝ์  ํ‘œ์ค€ ํฌ๋งท
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฒผ์œผ๋‚˜ ์‹ค์ œ๋กœ๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…
    • ๋”ฐ๋ผ์„œ JS์˜ ๊ฐ์ฒด๋กœ์จ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ตฌ๋ฌธ ๋ถ„์„(parsing)์ด ํ•„์ˆ˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” JSON์„ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ ์ œ๊ณต
    • JSON.parse()
      • JSON => ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
    • JSON.stringify()
      • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด => JSON

์˜ˆ์‹œ

image-20211106161640662

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all \u003cpre\u003e\u003ccode\u003e blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks"); } } catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); } })(); (function(){ try { var __m = "github.com"; var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Latest commit

History

History
514 lines (329 loc) ยท 12.2 KB

File metadata and controls

514 lines (329 loc) ยท 12.2 KB

๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด

๋ฐฐ์—ด(Arrays)

๋ฐฐ์—ด์˜ ์ •์˜์™€ ํŠน์ง•

  • ํ‚ค์™€ ์†์„ฑ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฐ์ฒด(object)
  • ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” ํŠน์ง•์ด ์žˆ์Œ
  • ์ฃผ๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ , 0์„ ํฌํ•จํ•œ ์–‘์˜ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ํŠน์ • ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” array.length ํ˜•ํƒœ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋Š” array.length - 1 ๋กœ ์ ‘๊ทผ

image-20211106151517307

I. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (1) - ๊ธฐ๋ณธํŽธ (๊ธฐ๋ณธ ๋ฐฐ์—ด ์กฐ์ž‘)

image-20211106151555718

I - 1. reverse

  • array.reverse()
    • ์›๋ณธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ

image-20211106151649779

I - 2. push & pop

  • array.push()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
  • array.pop()
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152033152

I - 3. unshift & shift

  • array.unshift()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
  • array.shift()
    • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152115109

I - 4. includes

  • array.includes(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋ณ„ ํ›„ ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106152159364

I - 5. indexOf

  • array.indexOf(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ํ›„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ๋กœ ์ฐพ์€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
    • ๋งŒ์•ฝ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ -1 ๋ฐ˜ํ™˜

image-20211106152620202

I - 6. join

  • array.join([separator])
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜
    • separator(๊ตฌ๋ถ„์ž)๋Š” ์„ ํƒ์ ์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ƒ๋žต ์‹œ ์‰ผํ‘œ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ

image-20211106152753676

์‹ค์Šต

/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 1]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ null ๊ฐ’์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.*/consthomeworks=['david.zip',null,'maria.zip','tom.zip',null]constresult=[]for(lethomeworkofhomeworks){if(homework){result.push(homework)}}console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 2]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜ ๋ฌธ์ž์—ด์„ ์™„์„ฑํ•˜์„ธ์š”.	'www.samsung.com/sec/buds/galaxy-buds-pro'*/constarr1=['www','samsung','com']constarr2=['galaxy','buds','pro']constarr3=['sec','buds']constmainURL=arr1.join('.')constmodel=arr2.join('-')arr3.unshift(mainURL)arr3.push(model)constresult=arr3.join('/')console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 3]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ๋ชจ๋“  'rainy' ์š”์†Œ๋ฅผ 'sunny'๋กœ ๊ต์ฒดํ•˜์„ธ์š”	- indexOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.*/constweather=['sunny','sunny','sunny','sunny','rainy','rainy','sunny']for(lettempofweather){if(temp=='rainy'){letidx=weather.indexOf('rainy')weather[idx]='sunny'}}letidx=weather.indexOf('rainy')while(idx>0){weather[idx]='sunny'idx=weather.indexOf('rainy')}console.log(weather)

II. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (2) - ์‹ฌํ™”ํŽธ (Array Helper Methods)

  • ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํŠน์ • ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ธ์ž๋กœ callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด ํŠน์ง•

image-20211106153029911

II - 1. forEach

  • array.forEach(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” 3๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ
      • element: ๋ฐฐ์—ด์˜ ์š”์†Œ
      • index: ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค
      • array: ๋ฐฐ์—ด ์ž์ฒด
    • ๋ฐ˜ํ™˜ ๊ฐ’(return)์ด ์—†๋Š” ๋ฉ”์„œ๋“œ

image-20211106153701098

II - 2. map

  • array.map(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์š”์†Œ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉ

image-20211106153757715

II - 3. filter

  • array.filter(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ธ ์š”์†Œ๋“ค๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋งํ•  ๋•Œ ์œ ์šฉ

image-20211106153900239

II - 4. reduce

  • array.reduce(callback(acc, element, [index[, array]])[, intialValue])

image-20211106154012229

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๊ฐ’(acc)์— ๋ˆ„์  ํ›„ ๋ฐ˜ํ™˜
  • reduce ๋ฉ”์„œ๋“œ์˜ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜
    • acc
      • ์ด์ „ callback ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ๋ˆ„์ ๋˜๋Š” ๋ณ€์ˆ˜
    • initialValue(optional)
      • ์ตœ์ดˆ callback ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ acc์— ํ• ๋‹น ๋˜๋Š” ๊ฐ’, default ๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’

์˜ˆ์‹œ

image-20211106154132959

image-20211106154155526

II - 5. find

  • array.find(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ด๋ฉด ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    • ์ฐพ๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์— ์—†์œผ๋ฉด undefined ๋ฐ˜ํ™˜

image-20211106154318481

II - 6. some

  • array.some(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106154411525

II - 7. every

  • array.every(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ์ฐธ ๋ฐ˜ํ™˜

image-20211106154458758

์‹ค์Šต

/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 1] ์†๋ ฅ(distance/time)์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด speeds๋ฅผ ๋งŒ๋“œ์„ธ์š”.*/consttrips=[{distance: 34,time: 10},{distance: 90,time: 50},{distance: 59,time: 25},]constspeeds=trips.map((trip)=>{returntrip.distance/trip.time})console.log(speeds)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 2] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํŠน์ • ๋ฌธ์ž(query)๊ฐ€ ํฌํ•จ๋˜๋Š” ์š”์†Œ๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.*/constlanguages=['python','javascript','html','java']constquery='java'constresults=languages.filter((language)=>{returnlanguage.includes(query)})console.log(results)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 3] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ์„ธ์š”. { smith: 90, peter: 80, anna: 85, }*/constscores=[{name: 'smith',score: 90},{name: 'peter',score: 80},{name: 'anna',score: 85},]constresult=scores.reduce((res,score)=>{res[score.name]=score.scorereturnres},{})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 4] ์ฃผ์–ด์ง„ accounts ๋ฐฐ์—ด์—์„œ balance๊ฐ€ 24,000์ธ ์‚ฌ๋žŒ์„ ์ฐพ์œผ์„ธ์š”.*/constaccounts=[{name: 'justin',balance: 1200},{name: 'harry',balance: 50000},{name: 'jason',balance: 24000},]constresult=accounts.find((account)=>{returnaccount.balance===24000})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 5] ์ฃผ์–ด์ง„ requests ๋ฐฐ์—ด์—์„œ status๊ฐ€ pending์ธ ์š”์ฒญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.*/constrequests=[{url: '/photos',status: 'complete'},{url: '/albums',status: 'pending'},{url: '/users',status: 'failed'},]constresult=requests.some((req)=>{returnreq.status==='pending'})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 6] ์ฃผ์–ด์ง„ users ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ชจ๋“  ์œ ์ €์˜ ์ƒํƒœ๊ฐ€ submmited์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์„ธ์š”.*/constusers=[{username: 'eric',submitted: true},{username: 'justin',submitted: true},{username: 'james',submitted: false},]constresult=users.every((user)=>{returnuser.submitted})console.log(result)constkey='regions'constvalue=['๊ตฌ๋ฏธ','๊ด‘์ฃผ','๋Œ€์ „','์„œ์šธ','๋ถ€์šธ๊ฒฝ']constloca={[key]: value,}console.log(loca)

๊ฐ์ฒด (Objects)

๊ฐ์ฒด์˜ ์ •์˜์™€ ํŠน์ง•

  • ๊ฐ์ฒด๋Š” ์†์„ฑ(property)์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€์— key์™€ value์˜ ์Œ์œผ๋กœ ํ‘œํ˜„
  • key๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ
  • value๋Š” ๋ชจ๋“  ํƒ€์ž… ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด ์š”์†Œ ์ ‘๊ทผ์€ ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ€๋Šฅ
    • key ์ด๋ฆ„์— ๋„์–ด์“ฐ๊ธฐ ๊ฐ™์€ ๊ตฌ๋ถ„์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋Œ€๊ด€ํ˜ธ ์ ‘๊ทผ๋งŒ ๊ฐ€๋Šฅ

I. ๊ฐ์ฒด ๊ด€๋ จ ES6 ๋ฌธ๋ฒ•

I - 1. ์†์„ฑ๋ช… ์ถ•์•ฝ (shorthand)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์™€ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถ•์•ฝ ๊ฐ€๋Šฅ

image-20211106154839156

I - 2. ๋ฉ”์„œ๋“œ๋ช… ์ถ•์•ฝ(shorthand)

  • ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ function ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ

image-20211106160105850

I - 3. ๊ณ„์‚ฐ๋œ ์†์„ฑ(computed property name)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์˜ ์ด๋ฆ„์„ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ

image-20211106160915728

I - 4. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (destructing assignment)

  • ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์†์„ฑ์„ ๋ณ€์ˆ˜์— ์‰ฝ๊ฒŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•

image-20211106161339931

์‹ค์Šต

/* [Object ์š”์†Œ ์ ‘๊ทผ ์—ฐ์Šต]	์ฃผ์–ด์ง„ ๊ฐ์ฒด์—์„œ items์˜ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ์˜ name์„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์„ธ์š”.*/constdata={id: 42,items: [{id: 1,name: 'foo',},{id: 2,name: 'bar',},],}constresult=data.items[0].nameconsole.log(result)/*[Object ์ถ•์•ฝ ๋ฌธ๋ฒ•]์•„๋ž˜ ๋ณ€์ˆ˜๋“ค์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๋Š” Object๋ฅผ ์ถ•์•ฝ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.*/constusername='hailey'constcontact='010-1234-5678'constresult={
username,
contact
}console.log(result)/*[Object Destructuring]์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ Object ์ถ•์•ฝ ๋ฌธ๋ฒ•๊ณผ, destructuring์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ž‘์„ฑํ•˜์„ธ์š”.*/constusers=[{name: 'hailey',contact: '010-1234-5678',},{name: 'paul',contact: '010-5678-1234',},]// ๋ณ€๊ฒฝ ํ›„functionsaveUserData(users){// Destructuring// const { name, contact } = userconstuserData=users.map(({ name, contact },id)=>{// ์ถ•์•ฝ ๋ฌธ๋ฒ• ํ‚ค์™€ ๋ฒจ๋ฅ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ํ•˜๋‚˜๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ!return{id, name, contact}})returnuserData}// ๋ณ€๊ฒฝ ์ „functionsaveUserData(users){constuserData=users.map((user,index)=>{return{id: index,name: user.name,contact: user.contact}})returnuserData}

II. JSON (JavaScript Object Notation)

  • key-value์Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ์–ธ์–ด ๋…๋ฆฝ์  ํ‘œ์ค€ ํฌ๋งท
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฒผ์œผ๋‚˜ ์‹ค์ œ๋กœ๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…
    • ๋”ฐ๋ผ์„œ JS์˜ ๊ฐ์ฒด๋กœ์จ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ตฌ๋ฌธ ๋ถ„์„(parsing)์ด ํ•„์ˆ˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” JSON์„ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ ์ œ๊ณต
    • JSON.parse()
      • JSON => ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
    • JSON.stringify()
      • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด => JSON

์˜ˆ์‹œ

image-20211106161640662

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
514 lines (329 loc) ยท 12.2 KB

File metadata and controls

514 lines (329 loc) ยท 12.2 KB

๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด

๋ฐฐ์—ด(Arrays)

๋ฐฐ์—ด์˜ ์ •์˜์™€ ํŠน์ง•

  • ํ‚ค์™€ ์†์„ฑ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฐ์ฒด(object)
  • ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” ํŠน์ง•์ด ์žˆ์Œ
  • ์ฃผ๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ , 0์„ ํฌํ•จํ•œ ์–‘์˜ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ํŠน์ • ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” array.length ํ˜•ํƒœ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋Š” array.length - 1 ๋กœ ์ ‘๊ทผ

image-20211106151517307

I. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (1) - ๊ธฐ๋ณธํŽธ (๊ธฐ๋ณธ ๋ฐฐ์—ด ์กฐ์ž‘)

image-20211106151555718

I - 1. reverse

  • array.reverse()
    • ์›๋ณธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ

image-20211106151649779

I - 2. push & pop

  • array.push()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
  • array.pop()
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152033152

I - 3. unshift & shift

  • array.unshift()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
  • array.shift()
    • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152115109

I - 4. includes

  • array.includes(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋ณ„ ํ›„ ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106152159364

I - 5. indexOf

  • array.indexOf(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ํ›„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ๋กœ ์ฐพ์€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
    • ๋งŒ์•ฝ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ -1 ๋ฐ˜ํ™˜

image-20211106152620202

I - 6. join

  • array.join([separator])
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜
    • separator(๊ตฌ๋ถ„์ž)๋Š” ์„ ํƒ์ ์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ƒ๋žต ์‹œ ์‰ผํ‘œ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ

image-20211106152753676

์‹ค์Šต

/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 1]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ null ๊ฐ’์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.*/consthomeworks=['david.zip',null,'maria.zip','tom.zip',null]constresult=[]for(lethomeworkofhomeworks){if(homework){result.push(homework)}}console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 2]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜ ๋ฌธ์ž์—ด์„ ์™„์„ฑํ•˜์„ธ์š”.	'www.samsung.com/sec/buds/galaxy-buds-pro'*/constarr1=['www','samsung','com']constarr2=['galaxy','buds','pro']constarr3=['sec','buds']constmainURL=arr1.join('.')constmodel=arr2.join('-')arr3.unshift(mainURL)arr3.push(model)constresult=arr3.join('/')console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 3]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ๋ชจ๋“  'rainy' ์š”์†Œ๋ฅผ 'sunny'๋กœ ๊ต์ฒดํ•˜์„ธ์š”	- indexOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.*/constweather=['sunny','sunny','sunny','sunny','rainy','rainy','sunny']for(lettempofweather){if(temp=='rainy'){letidx=weather.indexOf('rainy')weather[idx]='sunny'}}letidx=weather.indexOf('rainy')while(idx>0){weather[idx]='sunny'idx=weather.indexOf('rainy')}console.log(weather)

II. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (2) - ์‹ฌํ™”ํŽธ (Array Helper Methods)

  • ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํŠน์ • ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ธ์ž๋กœ callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด ํŠน์ง•

image-20211106153029911

II - 1. forEach

  • array.forEach(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” 3๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ
      • element: ๋ฐฐ์—ด์˜ ์š”์†Œ
      • index: ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค
      • array: ๋ฐฐ์—ด ์ž์ฒด
    • ๋ฐ˜ํ™˜ ๊ฐ’(return)์ด ์—†๋Š” ๋ฉ”์„œ๋“œ

image-20211106153701098

II - 2. map

  • array.map(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์š”์†Œ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉ

image-20211106153757715

II - 3. filter

  • array.filter(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ธ ์š”์†Œ๋“ค๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋งํ•  ๋•Œ ์œ ์šฉ

image-20211106153900239

II - 4. reduce

  • array.reduce(callback(acc, element, [index[, array]])[, intialValue])

image-20211106154012229

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๊ฐ’(acc)์— ๋ˆ„์  ํ›„ ๋ฐ˜ํ™˜
  • reduce ๋ฉ”์„œ๋“œ์˜ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜
    • acc
      • ์ด์ „ callback ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ๋ˆ„์ ๋˜๋Š” ๋ณ€์ˆ˜
    • initialValue(optional)
      • ์ตœ์ดˆ callback ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ acc์— ํ• ๋‹น ๋˜๋Š” ๊ฐ’, default ๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’

์˜ˆ์‹œ

image-20211106154132959

image-20211106154155526

II - 5. find

  • array.find(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ด๋ฉด ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    • ์ฐพ๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์— ์—†์œผ๋ฉด undefined ๋ฐ˜ํ™˜

image-20211106154318481

II - 6. some

  • array.some(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106154411525

II - 7. every

  • array.every(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ์ฐธ ๋ฐ˜ํ™˜

image-20211106154458758

์‹ค์Šต

/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 1] ์†๋ ฅ(distance/time)์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด speeds๋ฅผ ๋งŒ๋“œ์„ธ์š”.*/consttrips=[{distance: 34,time: 10},{distance: 90,time: 50},{distance: 59,time: 25},]constspeeds=trips.map((trip)=>{returntrip.distance/trip.time})console.log(speeds)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 2] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํŠน์ • ๋ฌธ์ž(query)๊ฐ€ ํฌํ•จ๋˜๋Š” ์š”์†Œ๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.*/constlanguages=['python','javascript','html','java']constquery='java'constresults=languages.filter((language)=>{returnlanguage.includes(query)})console.log(results)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 3] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ์„ธ์š”. { smith: 90, peter: 80, anna: 85, }*/constscores=[{name: 'smith',score: 90},{name: 'peter',score: 80},{name: 'anna',score: 85},]constresult=scores.reduce((res,score)=>{res[score.name]=score.scorereturnres},{})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 4] ์ฃผ์–ด์ง„ accounts ๋ฐฐ์—ด์—์„œ balance๊ฐ€ 24,000์ธ ์‚ฌ๋žŒ์„ ์ฐพ์œผ์„ธ์š”.*/constaccounts=[{name: 'justin',balance: 1200},{name: 'harry',balance: 50000},{name: 'jason',balance: 24000},]constresult=accounts.find((account)=>{returnaccount.balance===24000})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 5] ์ฃผ์–ด์ง„ requests ๋ฐฐ์—ด์—์„œ status๊ฐ€ pending์ธ ์š”์ฒญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.*/constrequests=[{url: '/photos',status: 'complete'},{url: '/albums',status: 'pending'},{url: '/users',status: 'failed'},]constresult=requests.some((req)=>{returnreq.status==='pending'})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 6] ์ฃผ์–ด์ง„ users ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ชจ๋“  ์œ ์ €์˜ ์ƒํƒœ๊ฐ€ submmited์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์„ธ์š”.*/constusers=[{username: 'eric',submitted: true},{username: 'justin',submitted: true},{username: 'james',submitted: false},]constresult=users.every((user)=>{returnuser.submitted})console.log(result)constkey='regions'constvalue=['๊ตฌ๋ฏธ','๊ด‘์ฃผ','๋Œ€์ „','์„œ์šธ','๋ถ€์šธ๊ฒฝ']constloca={[key]: value,}console.log(loca)

๊ฐ์ฒด (Objects)

๊ฐ์ฒด์˜ ์ •์˜์™€ ํŠน์ง•

  • ๊ฐ์ฒด๋Š” ์†์„ฑ(property)์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€์— key์™€ value์˜ ์Œ์œผ๋กœ ํ‘œํ˜„
  • key๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ
  • value๋Š” ๋ชจ๋“  ํƒ€์ž… ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด ์š”์†Œ ์ ‘๊ทผ์€ ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ€๋Šฅ
    • key ์ด๋ฆ„์— ๋„์–ด์“ฐ๊ธฐ ๊ฐ™์€ ๊ตฌ๋ถ„์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋Œ€๊ด€ํ˜ธ ์ ‘๊ทผ๋งŒ ๊ฐ€๋Šฅ

I. ๊ฐ์ฒด ๊ด€๋ จ ES6 ๋ฌธ๋ฒ•

I - 1. ์†์„ฑ๋ช… ์ถ•์•ฝ (shorthand)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์™€ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถ•์•ฝ ๊ฐ€๋Šฅ

image-20211106154839156

I - 2. ๋ฉ”์„œ๋“œ๋ช… ์ถ•์•ฝ(shorthand)

  • ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ function ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ

image-20211106160105850

I - 3. ๊ณ„์‚ฐ๋œ ์†์„ฑ(computed property name)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์˜ ์ด๋ฆ„์„ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ

image-20211106160915728

I - 4. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (destructing assignment)

  • ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์†์„ฑ์„ ๋ณ€์ˆ˜์— ์‰ฝ๊ฒŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•

image-20211106161339931

์‹ค์Šต

/* [Object ์š”์†Œ ์ ‘๊ทผ ์—ฐ์Šต]	์ฃผ์–ด์ง„ ๊ฐ์ฒด์—์„œ items์˜ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ์˜ name์„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์„ธ์š”.*/constdata={id: 42,items: [{id: 1,name: 'foo',},{id: 2,name: 'bar',},],}constresult=data.items[0].nameconsole.log(result)/*[Object ์ถ•์•ฝ ๋ฌธ๋ฒ•]์•„๋ž˜ ๋ณ€์ˆ˜๋“ค์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๋Š” Object๋ฅผ ์ถ•์•ฝ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.*/constusername='hailey'constcontact='010-1234-5678'constresult={
username,
contact
}console.log(result)/*[Object Destructuring]์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ Object ์ถ•์•ฝ ๋ฌธ๋ฒ•๊ณผ, destructuring์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ž‘์„ฑํ•˜์„ธ์š”.*/constusers=[{name: 'hailey',contact: '010-1234-5678',},{name: 'paul',contact: '010-5678-1234',},]// ๋ณ€๊ฒฝ ํ›„functionsaveUserData(users){// Destructuring// const { name, contact } = userconstuserData=users.map(({ name, contact },id)=>{// ์ถ•์•ฝ ๋ฌธ๋ฒ• ํ‚ค์™€ ๋ฒจ๋ฅ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ํ•˜๋‚˜๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ!return{id, name, contact}})returnuserData}// ๋ณ€๊ฒฝ ์ „functionsaveUserData(users){constuserData=users.map((user,index)=>{return{id: index,name: user.name,contact: user.contact}})returnuserData}

II. JSON (JavaScript Object Notation)

  • key-value์Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ์–ธ์–ด ๋…๋ฆฝ์  ํ‘œ์ค€ ํฌ๋งท
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฒผ์œผ๋‚˜ ์‹ค์ œ๋กœ๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…
    • ๋”ฐ๋ผ์„œ JS์˜ ๊ฐ์ฒด๋กœ์จ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ตฌ๋ฌธ ๋ถ„์„(parsing)์ด ํ•„์ˆ˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” JSON์„ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ ์ œ๊ณต
    • JSON.parse()
      • JSON => ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
    • JSON.stringify()
      • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด => JSON

์˜ˆ์‹œ

image-20211106161640662

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length \u003e 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
514 lines (329 loc) ยท 12.2 KB

File metadata and controls

514 lines (329 loc) ยท 12.2 KB

๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด

๋ฐฐ์—ด(Arrays)

๋ฐฐ์—ด์˜ ์ •์˜์™€ ํŠน์ง•

  • ํ‚ค์™€ ์†์„ฑ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฐ์ฒด(object)
  • ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” ํŠน์ง•์ด ์žˆ์Œ
  • ์ฃผ๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ , 0์„ ํฌํ•จํ•œ ์–‘์˜ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ํŠน์ • ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” array.length ํ˜•ํƒœ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋Š” array.length - 1 ๋กœ ์ ‘๊ทผ

image-20211106151517307

I. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (1) - ๊ธฐ๋ณธํŽธ (๊ธฐ๋ณธ ๋ฐฐ์—ด ์กฐ์ž‘)

image-20211106151555718

I - 1. reverse

  • array.reverse()
    • ์›๋ณธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ

image-20211106151649779

I - 2. push & pop

  • array.push()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
  • array.pop()
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152033152

I - 3. unshift & shift

  • array.unshift()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
  • array.shift()
    • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152115109

I - 4. includes

  • array.includes(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋ณ„ ํ›„ ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106152159364

I - 5. indexOf

  • array.indexOf(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ํ›„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ๋กœ ์ฐพ์€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
    • ๋งŒ์•ฝ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ -1 ๋ฐ˜ํ™˜

image-20211106152620202

I - 6. join

  • array.join([separator])
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜
    • separator(๊ตฌ๋ถ„์ž)๋Š” ์„ ํƒ์ ์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ƒ๋žต ์‹œ ์‰ผํ‘œ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ

image-20211106152753676

์‹ค์Šต

/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 1]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ null ๊ฐ’์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.*/consthomeworks=['david.zip',null,'maria.zip','tom.zip',null]constresult=[]for(lethomeworkofhomeworks){if(homework){result.push(homework)}}console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 2]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜ ๋ฌธ์ž์—ด์„ ์™„์„ฑํ•˜์„ธ์š”.	'www.samsung.com/sec/buds/galaxy-buds-pro'*/constarr1=['www','samsung','com']constarr2=['galaxy','buds','pro']constarr3=['sec','buds']constmainURL=arr1.join('.')constmodel=arr2.join('-')arr3.unshift(mainURL)arr3.push(model)constresult=arr3.join('/')console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 3]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ๋ชจ๋“  'rainy' ์š”์†Œ๋ฅผ 'sunny'๋กœ ๊ต์ฒดํ•˜์„ธ์š”	- indexOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.*/constweather=['sunny','sunny','sunny','sunny','rainy','rainy','sunny']for(lettempofweather){if(temp=='rainy'){letidx=weather.indexOf('rainy')weather[idx]='sunny'}}letidx=weather.indexOf('rainy')while(idx>0){weather[idx]='sunny'idx=weather.indexOf('rainy')}console.log(weather)

II. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (2) - ์‹ฌํ™”ํŽธ (Array Helper Methods)

  • ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํŠน์ • ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ธ์ž๋กœ callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด ํŠน์ง•

image-20211106153029911

II - 1. forEach

  • array.forEach(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” 3๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ
      • element: ๋ฐฐ์—ด์˜ ์š”์†Œ
      • index: ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค
      • array: ๋ฐฐ์—ด ์ž์ฒด
    • ๋ฐ˜ํ™˜ ๊ฐ’(return)์ด ์—†๋Š” ๋ฉ”์„œ๋“œ

image-20211106153701098

II - 2. map

  • array.map(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์š”์†Œ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉ

image-20211106153757715

II - 3. filter

  • array.filter(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ธ ์š”์†Œ๋“ค๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋งํ•  ๋•Œ ์œ ์šฉ

image-20211106153900239

II - 4. reduce

  • array.reduce(callback(acc, element, [index[, array]])[, intialValue])

image-20211106154012229

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๊ฐ’(acc)์— ๋ˆ„์  ํ›„ ๋ฐ˜ํ™˜
  • reduce ๋ฉ”์„œ๋“œ์˜ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜
    • acc
      • ์ด์ „ callback ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ๋ˆ„์ ๋˜๋Š” ๋ณ€์ˆ˜
    • initialValue(optional)
      • ์ตœ์ดˆ callback ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ acc์— ํ• ๋‹น ๋˜๋Š” ๊ฐ’, default ๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’

์˜ˆ์‹œ

image-20211106154132959

image-20211106154155526

II - 5. find

  • array.find(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ด๋ฉด ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    • ์ฐพ๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์— ์—†์œผ๋ฉด undefined ๋ฐ˜ํ™˜

image-20211106154318481

II - 6. some

  • array.some(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106154411525

II - 7. every

  • array.every(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ์ฐธ ๋ฐ˜ํ™˜

image-20211106154458758

์‹ค์Šต

/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 1] ์†๋ ฅ(distance/time)์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด speeds๋ฅผ ๋งŒ๋“œ์„ธ์š”.*/consttrips=[{distance: 34,time: 10},{distance: 90,time: 50},{distance: 59,time: 25},]constspeeds=trips.map((trip)=>{returntrip.distance/trip.time})console.log(speeds)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 2] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํŠน์ • ๋ฌธ์ž(query)๊ฐ€ ํฌํ•จ๋˜๋Š” ์š”์†Œ๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.*/constlanguages=['python','javascript','html','java']constquery='java'constresults=languages.filter((language)=>{returnlanguage.includes(query)})console.log(results)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 3] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ์„ธ์š”. { smith: 90, peter: 80, anna: 85, }*/constscores=[{name: 'smith',score: 90},{name: 'peter',score: 80},{name: 'anna',score: 85},]constresult=scores.reduce((res,score)=>{res[score.name]=score.scorereturnres},{})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 4] ์ฃผ์–ด์ง„ accounts ๋ฐฐ์—ด์—์„œ balance๊ฐ€ 24,000์ธ ์‚ฌ๋žŒ์„ ์ฐพ์œผ์„ธ์š”.*/constaccounts=[{name: 'justin',balance: 1200},{name: 'harry',balance: 50000},{name: 'jason',balance: 24000},]constresult=accounts.find((account)=>{returnaccount.balance===24000})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 5] ์ฃผ์–ด์ง„ requests ๋ฐฐ์—ด์—์„œ status๊ฐ€ pending์ธ ์š”์ฒญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.*/constrequests=[{url: '/photos',status: 'complete'},{url: '/albums',status: 'pending'},{url: '/users',status: 'failed'},]constresult=requests.some((req)=>{returnreq.status==='pending'})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 6] ์ฃผ์–ด์ง„ users ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ชจ๋“  ์œ ์ €์˜ ์ƒํƒœ๊ฐ€ submmited์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์„ธ์š”.*/constusers=[{username: 'eric',submitted: true},{username: 'justin',submitted: true},{username: 'james',submitted: false},]constresult=users.every((user)=>{returnuser.submitted})console.log(result)constkey='regions'constvalue=['๊ตฌ๋ฏธ','๊ด‘์ฃผ','๋Œ€์ „','์„œ์šธ','๋ถ€์šธ๊ฒฝ']constloca={[key]: value,}console.log(loca)

๊ฐ์ฒด (Objects)

๊ฐ์ฒด์˜ ์ •์˜์™€ ํŠน์ง•

  • ๊ฐ์ฒด๋Š” ์†์„ฑ(property)์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€์— key์™€ value์˜ ์Œ์œผ๋กœ ํ‘œํ˜„
  • key๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ
  • value๋Š” ๋ชจ๋“  ํƒ€์ž… ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด ์š”์†Œ ์ ‘๊ทผ์€ ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ€๋Šฅ
    • key ์ด๋ฆ„์— ๋„์–ด์“ฐ๊ธฐ ๊ฐ™์€ ๊ตฌ๋ถ„์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋Œ€๊ด€ํ˜ธ ์ ‘๊ทผ๋งŒ ๊ฐ€๋Šฅ

I. ๊ฐ์ฒด ๊ด€๋ จ ES6 ๋ฌธ๋ฒ•

I - 1. ์†์„ฑ๋ช… ์ถ•์•ฝ (shorthand)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์™€ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถ•์•ฝ ๊ฐ€๋Šฅ

image-20211106154839156

I - 2. ๋ฉ”์„œ๋“œ๋ช… ์ถ•์•ฝ(shorthand)

  • ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ function ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ

image-20211106160105850

I - 3. ๊ณ„์‚ฐ๋œ ์†์„ฑ(computed property name)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์˜ ์ด๋ฆ„์„ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ

image-20211106160915728

I - 4. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (destructing assignment)

  • ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์†์„ฑ์„ ๋ณ€์ˆ˜์— ์‰ฝ๊ฒŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•

image-20211106161339931

์‹ค์Šต

/* [Object ์š”์†Œ ์ ‘๊ทผ ์—ฐ์Šต]	์ฃผ์–ด์ง„ ๊ฐ์ฒด์—์„œ items์˜ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ์˜ name์„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์„ธ์š”.*/constdata={id: 42,items: [{id: 1,name: 'foo',},{id: 2,name: 'bar',},],}constresult=data.items[0].nameconsole.log(result)/*[Object ์ถ•์•ฝ ๋ฌธ๋ฒ•]์•„๋ž˜ ๋ณ€์ˆ˜๋“ค์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๋Š” Object๋ฅผ ์ถ•์•ฝ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.*/constusername='hailey'constcontact='010-1234-5678'constresult={
username,
contact
}console.log(result)/*[Object Destructuring]์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ Object ์ถ•์•ฝ ๋ฌธ๋ฒ•๊ณผ, destructuring์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ž‘์„ฑํ•˜์„ธ์š”.*/constusers=[{name: 'hailey',contact: '010-1234-5678',},{name: 'paul',contact: '010-5678-1234',},]// ๋ณ€๊ฒฝ ํ›„functionsaveUserData(users){// Destructuring// const { name, contact } = userconstuserData=users.map(({ name, contact },id)=>{// ์ถ•์•ฝ ๋ฌธ๋ฒ• ํ‚ค์™€ ๋ฒจ๋ฅ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ํ•˜๋‚˜๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ!return{id, name, contact}})returnuserData}// ๋ณ€๊ฒฝ ์ „functionsaveUserData(users){constuserData=users.map((user,index)=>{return{id: index,name: user.name,contact: user.contact}})returnuserData}

II. JSON (JavaScript Object Notation)

  • key-value์Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ์–ธ์–ด ๋…๋ฆฝ์  ํ‘œ์ค€ ํฌ๋งท
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฒผ์œผ๋‚˜ ์‹ค์ œ๋กœ๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…
    • ๋”ฐ๋ผ์„œ JS์˜ ๊ฐ์ฒด๋กœ์จ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ตฌ๋ฌธ ๋ถ„์„(parsing)์ด ํ•„์ˆ˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” JSON์„ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ ์ œ๊ณต
    • JSON.parse()
      • JSON => ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
    • JSON.stringify()
      • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด => JSON

์˜ˆ์‹œ

image-20211106161640662

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Latest commit

History

History
514 lines (329 loc) ยท 12.2 KB

File metadata and controls

514 lines (329 loc) ยท 12.2 KB

๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด

๋ฐฐ์—ด(Arrays)

๋ฐฐ์—ด์˜ ์ •์˜์™€ ํŠน์ง•

  • ํ‚ค์™€ ์†์„ฑ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฐ์ฒด(object)
  • ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” ํŠน์ง•์ด ์žˆ์Œ
  • ์ฃผ๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ , 0์„ ํฌํ•จํ•œ ์–‘์˜ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ํŠน์ • ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” array.length ํ˜•ํƒœ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋Š” array.length - 1 ๋กœ ์ ‘๊ทผ

image-20211106151517307

I. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (1) - ๊ธฐ๋ณธํŽธ (๊ธฐ๋ณธ ๋ฐฐ์—ด ์กฐ์ž‘)

image-20211106151555718

I - 1. reverse

  • array.reverse()
    • ์›๋ณธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ

image-20211106151649779

I - 2. push & pop

  • array.push()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
  • array.pop()
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152033152

I - 3. unshift & shift

  • array.unshift()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
  • array.shift()
    • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152115109

I - 4. includes

  • array.includes(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋ณ„ ํ›„ ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106152159364

I - 5. indexOf

  • array.indexOf(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ํ›„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ๋กœ ์ฐพ์€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
    • ๋งŒ์•ฝ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ -1 ๋ฐ˜ํ™˜

image-20211106152620202

I - 6. join

  • array.join([separator])
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜
    • separator(๊ตฌ๋ถ„์ž)๋Š” ์„ ํƒ์ ์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ƒ๋žต ์‹œ ์‰ผํ‘œ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ

image-20211106152753676

์‹ค์Šต

/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 1]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ null ๊ฐ’์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.*/consthomeworks=['david.zip',null,'maria.zip','tom.zip',null]constresult=[]for(lethomeworkofhomeworks){if(homework){result.push(homework)}}console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 2]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜ ๋ฌธ์ž์—ด์„ ์™„์„ฑํ•˜์„ธ์š”.	'www.samsung.com/sec/buds/galaxy-buds-pro'*/constarr1=['www','samsung','com']constarr2=['galaxy','buds','pro']constarr3=['sec','buds']constmainURL=arr1.join('.')constmodel=arr2.join('-')arr3.unshift(mainURL)arr3.push(model)constresult=arr3.join('/')console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 3]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ๋ชจ๋“  'rainy' ์š”์†Œ๋ฅผ 'sunny'๋กœ ๊ต์ฒดํ•˜์„ธ์š”	- indexOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.*/constweather=['sunny','sunny','sunny','sunny','rainy','rainy','sunny']for(lettempofweather){if(temp=='rainy'){letidx=weather.indexOf('rainy')weather[idx]='sunny'}}letidx=weather.indexOf('rainy')while(idx>0){weather[idx]='sunny'idx=weather.indexOf('rainy')}console.log(weather)

II. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (2) - ์‹ฌํ™”ํŽธ (Array Helper Methods)

  • ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํŠน์ • ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ธ์ž๋กœ callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด ํŠน์ง•

image-20211106153029911

II - 1. forEach

  • array.forEach(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” 3๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ
      • element: ๋ฐฐ์—ด์˜ ์š”์†Œ
      • index: ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค
      • array: ๋ฐฐ์—ด ์ž์ฒด
    • ๋ฐ˜ํ™˜ ๊ฐ’(return)์ด ์—†๋Š” ๋ฉ”์„œ๋“œ

image-20211106153701098

II - 2. map

  • array.map(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์š”์†Œ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉ

image-20211106153757715

II - 3. filter

  • array.filter(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ธ ์š”์†Œ๋“ค๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋งํ•  ๋•Œ ์œ ์šฉ

image-20211106153900239

II - 4. reduce

  • array.reduce(callback(acc, element, [index[, array]])[, intialValue])

image-20211106154012229

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๊ฐ’(acc)์— ๋ˆ„์  ํ›„ ๋ฐ˜ํ™˜
  • reduce ๋ฉ”์„œ๋“œ์˜ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜
    • acc
      • ์ด์ „ callback ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ๋ˆ„์ ๋˜๋Š” ๋ณ€์ˆ˜
    • initialValue(optional)
      • ์ตœ์ดˆ callback ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ acc์— ํ• ๋‹น ๋˜๋Š” ๊ฐ’, default ๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’

์˜ˆ์‹œ

image-20211106154132959

image-20211106154155526

II - 5. find

  • array.find(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ด๋ฉด ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    • ์ฐพ๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์— ์—†์œผ๋ฉด undefined ๋ฐ˜ํ™˜

image-20211106154318481

II - 6. some

  • array.some(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106154411525

II - 7. every

  • array.every(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ์ฐธ ๋ฐ˜ํ™˜

image-20211106154458758

์‹ค์Šต

/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 1] ์†๋ ฅ(distance/time)์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด speeds๋ฅผ ๋งŒ๋“œ์„ธ์š”.*/consttrips=[{distance: 34,time: 10},{distance: 90,time: 50},{distance: 59,time: 25},]constspeeds=trips.map((trip)=>{returntrip.distance/trip.time})console.log(speeds)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 2] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํŠน์ • ๋ฌธ์ž(query)๊ฐ€ ํฌํ•จ๋˜๋Š” ์š”์†Œ๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.*/constlanguages=['python','javascript','html','java']constquery='java'constresults=languages.filter((language)=>{returnlanguage.includes(query)})console.log(results)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 3] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ์„ธ์š”. { smith: 90, peter: 80, anna: 85, }*/constscores=[{name: 'smith',score: 90},{name: 'peter',score: 80},{name: 'anna',score: 85},]constresult=scores.reduce((res,score)=>{res[score.name]=score.scorereturnres},{})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 4] ์ฃผ์–ด์ง„ accounts ๋ฐฐ์—ด์—์„œ balance๊ฐ€ 24,000์ธ ์‚ฌ๋žŒ์„ ์ฐพ์œผ์„ธ์š”.*/constaccounts=[{name: 'justin',balance: 1200},{name: 'harry',balance: 50000},{name: 'jason',balance: 24000},]constresult=accounts.find((account)=>{returnaccount.balance===24000})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 5] ์ฃผ์–ด์ง„ requests ๋ฐฐ์—ด์—์„œ status๊ฐ€ pending์ธ ์š”์ฒญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.*/constrequests=[{url: '/photos',status: 'complete'},{url: '/albums',status: 'pending'},{url: '/users',status: 'failed'},]constresult=requests.some((req)=>{returnreq.status==='pending'})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 6] ์ฃผ์–ด์ง„ users ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ชจ๋“  ์œ ์ €์˜ ์ƒํƒœ๊ฐ€ submmited์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์„ธ์š”.*/constusers=[{username: 'eric',submitted: true},{username: 'justin',submitted: true},{username: 'james',submitted: false},]constresult=users.every((user)=>{returnuser.submitted})console.log(result)constkey='regions'constvalue=['๊ตฌ๋ฏธ','๊ด‘์ฃผ','๋Œ€์ „','์„œ์šธ','๋ถ€์šธ๊ฒฝ']constloca={[key]: value,}console.log(loca)

๊ฐ์ฒด (Objects)

๊ฐ์ฒด์˜ ์ •์˜์™€ ํŠน์ง•

  • ๊ฐ์ฒด๋Š” ์†์„ฑ(property)์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€์— key์™€ value์˜ ์Œ์œผ๋กœ ํ‘œํ˜„
  • key๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ
  • value๋Š” ๋ชจ๋“  ํƒ€์ž… ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด ์š”์†Œ ์ ‘๊ทผ์€ ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ€๋Šฅ
    • key ์ด๋ฆ„์— ๋„์–ด์“ฐ๊ธฐ ๊ฐ™์€ ๊ตฌ๋ถ„์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋Œ€๊ด€ํ˜ธ ์ ‘๊ทผ๋งŒ ๊ฐ€๋Šฅ

I. ๊ฐ์ฒด ๊ด€๋ จ ES6 ๋ฌธ๋ฒ•

I - 1. ์†์„ฑ๋ช… ์ถ•์•ฝ (shorthand)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์™€ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถ•์•ฝ ๊ฐ€๋Šฅ

image-20211106154839156

I - 2. ๋ฉ”์„œ๋“œ๋ช… ์ถ•์•ฝ(shorthand)

  • ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ function ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ

image-20211106160105850

I - 3. ๊ณ„์‚ฐ๋œ ์†์„ฑ(computed property name)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์˜ ์ด๋ฆ„์„ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ

image-20211106160915728

I - 4. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (destructing assignment)

  • ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์†์„ฑ์„ ๋ณ€์ˆ˜์— ์‰ฝ๊ฒŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•

image-20211106161339931

์‹ค์Šต

/* [Object ์š”์†Œ ์ ‘๊ทผ ์—ฐ์Šต]	์ฃผ์–ด์ง„ ๊ฐ์ฒด์—์„œ items์˜ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ์˜ name์„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์„ธ์š”.*/constdata={id: 42,items: [{id: 1,name: 'foo',},{id: 2,name: 'bar',},],}constresult=data.items[0].nameconsole.log(result)/*[Object ์ถ•์•ฝ ๋ฌธ๋ฒ•]์•„๋ž˜ ๋ณ€์ˆ˜๋“ค์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๋Š” Object๋ฅผ ์ถ•์•ฝ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.*/constusername='hailey'constcontact='010-1234-5678'constresult={
username,
contact
}console.log(result)/*[Object Destructuring]์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ Object ์ถ•์•ฝ ๋ฌธ๋ฒ•๊ณผ, destructuring์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ž‘์„ฑํ•˜์„ธ์š”.*/constusers=[{name: 'hailey',contact: '010-1234-5678',},{name: 'paul',contact: '010-5678-1234',},]// ๋ณ€๊ฒฝ ํ›„functionsaveUserData(users){// Destructuring// const { name, contact } = userconstuserData=users.map(({ name, contact },id)=>{// ์ถ•์•ฝ ๋ฌธ๋ฒ• ํ‚ค์™€ ๋ฒจ๋ฅ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ํ•˜๋‚˜๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ!return{id, name, contact}})returnuserData}// ๋ณ€๊ฒฝ ์ „functionsaveUserData(users){constuserData=users.map((user,index)=>{return{id: index,name: user.name,contact: user.contact}})returnuserData}

II. JSON (JavaScript Object Notation)

  • key-value์Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ์–ธ์–ด ๋…๋ฆฝ์  ํ‘œ์ค€ ํฌ๋งท
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฒผ์œผ๋‚˜ ์‹ค์ œ๋กœ๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…
    • ๋”ฐ๋ผ์„œ JS์˜ ๊ฐ์ฒด๋กœ์จ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ตฌ๋ฌธ ๋ถ„์„(parsing)์ด ํ•„์ˆ˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” JSON์„ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ ์ œ๊ณต
    • JSON.parse()
      • JSON => ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
    • JSON.stringify()
      • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด => JSON

์˜ˆ์‹œ

image-20211106161640662

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
514 lines (329 loc) ยท 12.2 KB

File metadata and controls

514 lines (329 loc) ยท 12.2 KB

๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด

๋ฐฐ์—ด(Arrays)

๋ฐฐ์—ด์˜ ์ •์˜์™€ ํŠน์ง•

  • ํ‚ค์™€ ์†์„ฑ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฐ์ฒด(object)
  • ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” ํŠน์ง•์ด ์žˆ์Œ
  • ์ฃผ๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ , 0์„ ํฌํ•จํ•œ ์–‘์˜ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ํŠน์ • ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” array.length ํ˜•ํƒœ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋Š” array.length - 1 ๋กœ ์ ‘๊ทผ

image-20211106151517307

I. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (1) - ๊ธฐ๋ณธํŽธ (๊ธฐ๋ณธ ๋ฐฐ์—ด ์กฐ์ž‘)

image-20211106151555718

I - 1. reverse

  • array.reverse()
    • ์›๋ณธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ

image-20211106151649779

I - 2. push & pop

  • array.push()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
  • array.pop()
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152033152

I - 3. unshift & shift

  • array.unshift()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
  • array.shift()
    • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152115109

I - 4. includes

  • array.includes(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋ณ„ ํ›„ ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106152159364

I - 5. indexOf

  • array.indexOf(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ํ›„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ๋กœ ์ฐพ์€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
    • ๋งŒ์•ฝ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ -1 ๋ฐ˜ํ™˜

image-20211106152620202

I - 6. join

  • array.join([separator])
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜
    • separator(๊ตฌ๋ถ„์ž)๋Š” ์„ ํƒ์ ์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ƒ๋žต ์‹œ ์‰ผํ‘œ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ

image-20211106152753676

์‹ค์Šต

/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 1]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ null ๊ฐ’์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.*/consthomeworks=['david.zip',null,'maria.zip','tom.zip',null]constresult=[]for(lethomeworkofhomeworks){if(homework){result.push(homework)}}console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 2]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜ ๋ฌธ์ž์—ด์„ ์™„์„ฑํ•˜์„ธ์š”.	'www.samsung.com/sec/buds/galaxy-buds-pro'*/constarr1=['www','samsung','com']constarr2=['galaxy','buds','pro']constarr3=['sec','buds']constmainURL=arr1.join('.')constmodel=arr2.join('-')arr3.unshift(mainURL)arr3.push(model)constresult=arr3.join('/')console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 3]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ๋ชจ๋“  'rainy' ์š”์†Œ๋ฅผ 'sunny'๋กœ ๊ต์ฒดํ•˜์„ธ์š”	- indexOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.*/constweather=['sunny','sunny','sunny','sunny','rainy','rainy','sunny']for(lettempofweather){if(temp=='rainy'){letidx=weather.indexOf('rainy')weather[idx]='sunny'}}letidx=weather.indexOf('rainy')while(idx>0){weather[idx]='sunny'idx=weather.indexOf('rainy')}console.log(weather)

II. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (2) - ์‹ฌํ™”ํŽธ (Array Helper Methods)

  • ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํŠน์ • ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ธ์ž๋กœ callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด ํŠน์ง•

image-20211106153029911

II - 1. forEach

  • array.forEach(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” 3๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ
      • element: ๋ฐฐ์—ด์˜ ์š”์†Œ
      • index: ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค
      • array: ๋ฐฐ์—ด ์ž์ฒด
    • ๋ฐ˜ํ™˜ ๊ฐ’(return)์ด ์—†๋Š” ๋ฉ”์„œ๋“œ

image-20211106153701098

II - 2. map

  • array.map(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์š”์†Œ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉ

image-20211106153757715

II - 3. filter

  • array.filter(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ธ ์š”์†Œ๋“ค๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋งํ•  ๋•Œ ์œ ์šฉ

image-20211106153900239

II - 4. reduce

  • array.reduce(callback(acc, element, [index[, array]])[, intialValue])

image-20211106154012229

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๊ฐ’(acc)์— ๋ˆ„์  ํ›„ ๋ฐ˜ํ™˜
  • reduce ๋ฉ”์„œ๋“œ์˜ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜
    • acc
      • ์ด์ „ callback ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ๋ˆ„์ ๋˜๋Š” ๋ณ€์ˆ˜
    • initialValue(optional)
      • ์ตœ์ดˆ callback ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ acc์— ํ• ๋‹น ๋˜๋Š” ๊ฐ’, default ๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’

์˜ˆ์‹œ

image-20211106154132959

image-20211106154155526

II - 5. find

  • array.find(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ด๋ฉด ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    • ์ฐพ๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์— ์—†์œผ๋ฉด undefined ๋ฐ˜ํ™˜

image-20211106154318481

II - 6. some

  • array.some(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106154411525

II - 7. every

  • array.every(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ์ฐธ ๋ฐ˜ํ™˜

image-20211106154458758

์‹ค์Šต

/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 1] ์†๋ ฅ(distance/time)์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด speeds๋ฅผ ๋งŒ๋“œ์„ธ์š”.*/consttrips=[{distance: 34,time: 10},{distance: 90,time: 50},{distance: 59,time: 25},]constspeeds=trips.map((trip)=>{returntrip.distance/trip.time})console.log(speeds)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 2] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํŠน์ • ๋ฌธ์ž(query)๊ฐ€ ํฌํ•จ๋˜๋Š” ์š”์†Œ๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.*/constlanguages=['python','javascript','html','java']constquery='java'constresults=languages.filter((language)=>{returnlanguage.includes(query)})console.log(results)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 3] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ์„ธ์š”. { smith: 90, peter: 80, anna: 85, }*/constscores=[{name: 'smith',score: 90},{name: 'peter',score: 80},{name: 'anna',score: 85},]constresult=scores.reduce((res,score)=>{res[score.name]=score.scorereturnres},{})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 4] ์ฃผ์–ด์ง„ accounts ๋ฐฐ์—ด์—์„œ balance๊ฐ€ 24,000์ธ ์‚ฌ๋žŒ์„ ์ฐพ์œผ์„ธ์š”.*/constaccounts=[{name: 'justin',balance: 1200},{name: 'harry',balance: 50000},{name: 'jason',balance: 24000},]constresult=accounts.find((account)=>{returnaccount.balance===24000})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 5] ์ฃผ์–ด์ง„ requests ๋ฐฐ์—ด์—์„œ status๊ฐ€ pending์ธ ์š”์ฒญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.*/constrequests=[{url: '/photos',status: 'complete'},{url: '/albums',status: 'pending'},{url: '/users',status: 'failed'},]constresult=requests.some((req)=>{returnreq.status==='pending'})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 6] ์ฃผ์–ด์ง„ users ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ชจ๋“  ์œ ์ €์˜ ์ƒํƒœ๊ฐ€ submmited์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์„ธ์š”.*/constusers=[{username: 'eric',submitted: true},{username: 'justin',submitted: true},{username: 'james',submitted: false},]constresult=users.every((user)=>{returnuser.submitted})console.log(result)constkey='regions'constvalue=['๊ตฌ๋ฏธ','๊ด‘์ฃผ','๋Œ€์ „','์„œ์šธ','๋ถ€์šธ๊ฒฝ']constloca={[key]: value,}console.log(loca)

๊ฐ์ฒด (Objects)

๊ฐ์ฒด์˜ ์ •์˜์™€ ํŠน์ง•

  • ๊ฐ์ฒด๋Š” ์†์„ฑ(property)์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€์— key์™€ value์˜ ์Œ์œผ๋กœ ํ‘œํ˜„
  • key๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ
  • value๋Š” ๋ชจ๋“  ํƒ€์ž… ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด ์š”์†Œ ์ ‘๊ทผ์€ ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ€๋Šฅ
    • key ์ด๋ฆ„์— ๋„์–ด์“ฐ๊ธฐ ๊ฐ™์€ ๊ตฌ๋ถ„์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋Œ€๊ด€ํ˜ธ ์ ‘๊ทผ๋งŒ ๊ฐ€๋Šฅ

I. ๊ฐ์ฒด ๊ด€๋ จ ES6 ๋ฌธ๋ฒ•

I - 1. ์†์„ฑ๋ช… ์ถ•์•ฝ (shorthand)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์™€ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถ•์•ฝ ๊ฐ€๋Šฅ

image-20211106154839156

I - 2. ๋ฉ”์„œ๋“œ๋ช… ์ถ•์•ฝ(shorthand)

  • ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ function ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ

image-20211106160105850

I - 3. ๊ณ„์‚ฐ๋œ ์†์„ฑ(computed property name)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์˜ ์ด๋ฆ„์„ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ

image-20211106160915728

I - 4. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (destructing assignment)

  • ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์†์„ฑ์„ ๋ณ€์ˆ˜์— ์‰ฝ๊ฒŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•

image-20211106161339931

์‹ค์Šต

/* [Object ์š”์†Œ ์ ‘๊ทผ ์—ฐ์Šต]	์ฃผ์–ด์ง„ ๊ฐ์ฒด์—์„œ items์˜ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ์˜ name์„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์„ธ์š”.*/constdata={id: 42,items: [{id: 1,name: 'foo',},{id: 2,name: 'bar',},],}constresult=data.items[0].nameconsole.log(result)/*[Object ์ถ•์•ฝ ๋ฌธ๋ฒ•]์•„๋ž˜ ๋ณ€์ˆ˜๋“ค์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๋Š” Object๋ฅผ ์ถ•์•ฝ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.*/constusername='hailey'constcontact='010-1234-5678'constresult={
username,
contact
}console.log(result)/*[Object Destructuring]์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ Object ์ถ•์•ฝ ๋ฌธ๋ฒ•๊ณผ, destructuring์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ž‘์„ฑํ•˜์„ธ์š”.*/constusers=[{name: 'hailey',contact: '010-1234-5678',},{name: 'paul',contact: '010-5678-1234',},]// ๋ณ€๊ฒฝ ํ›„functionsaveUserData(users){// Destructuring// const { name, contact } = userconstuserData=users.map(({ name, contact },id)=>{// ์ถ•์•ฝ ๋ฌธ๋ฒ• ํ‚ค์™€ ๋ฒจ๋ฅ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ํ•˜๋‚˜๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ!return{id, name, contact}})returnuserData}// ๋ณ€๊ฒฝ ์ „functionsaveUserData(users){constuserData=users.map((user,index)=>{return{id: index,name: user.name,contact: user.contact}})returnuserData}

II. JSON (JavaScript Object Notation)

  • key-value์Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ์–ธ์–ด ๋…๋ฆฝ์  ํ‘œ์ค€ ํฌ๋งท
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฒผ์œผ๋‚˜ ์‹ค์ œ๋กœ๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…
    • ๋”ฐ๋ผ์„œ JS์˜ ๊ฐ์ฒด๋กœ์จ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ตฌ๋ฌธ ๋ถ„์„(parsing)์ด ํ•„์ˆ˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” JSON์„ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ ์ œ๊ณต
    • JSON.parse()
      • JSON => ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
    • JSON.stringify()
      • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด => JSON

์˜ˆ์‹œ

image-20211106161640662

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Latest commit

History

History
514 lines (329 loc) ยท 12.2 KB

File metadata and controls

514 lines (329 loc) ยท 12.2 KB

๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด

๋ฐฐ์—ด(Arrays)

๋ฐฐ์—ด์˜ ์ •์˜์™€ ํŠน์ง•

  • ํ‚ค์™€ ์†์„ฑ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฐ์ฒด(object)
  • ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” ํŠน์ง•์ด ์žˆ์Œ
  • ์ฃผ๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ , 0์„ ํฌํ•จํ•œ ์–‘์˜ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ํŠน์ • ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” array.length ํ˜•ํƒœ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋Š” array.length - 1 ๋กœ ์ ‘๊ทผ

image-20211106151517307

I. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (1) - ๊ธฐ๋ณธํŽธ (๊ธฐ๋ณธ ๋ฐฐ์—ด ์กฐ์ž‘)

image-20211106151555718

I - 1. reverse

  • array.reverse()
    • ์›๋ณธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ

image-20211106151649779

I - 2. push & pop

  • array.push()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
  • array.pop()
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152033152

I - 3. unshift & shift

  • array.unshift()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
  • array.shift()
    • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152115109

I - 4. includes

  • array.includes(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋ณ„ ํ›„ ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106152159364

I - 5. indexOf

  • array.indexOf(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ํ›„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ๋กœ ์ฐพ์€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
    • ๋งŒ์•ฝ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ -1 ๋ฐ˜ํ™˜

image-20211106152620202

I - 6. join

  • array.join([separator])
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜
    • separator(๊ตฌ๋ถ„์ž)๋Š” ์„ ํƒ์ ์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ƒ๋žต ์‹œ ์‰ผํ‘œ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ

image-20211106152753676

์‹ค์Šต

/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 1]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ null ๊ฐ’์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.*/consthomeworks=['david.zip',null,'maria.zip','tom.zip',null]constresult=[]for(lethomeworkofhomeworks){if(homework){result.push(homework)}}console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 2]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜ ๋ฌธ์ž์—ด์„ ์™„์„ฑํ•˜์„ธ์š”.	'www.samsung.com/sec/buds/galaxy-buds-pro'*/constarr1=['www','samsung','com']constarr2=['galaxy','buds','pro']constarr3=['sec','buds']constmainURL=arr1.join('.')constmodel=arr2.join('-')arr3.unshift(mainURL)arr3.push(model)constresult=arr3.join('/')console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 3]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ๋ชจ๋“  'rainy' ์š”์†Œ๋ฅผ 'sunny'๋กœ ๊ต์ฒดํ•˜์„ธ์š”	- indexOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.*/constweather=['sunny','sunny','sunny','sunny','rainy','rainy','sunny']for(lettempofweather){if(temp=='rainy'){letidx=weather.indexOf('rainy')weather[idx]='sunny'}}letidx=weather.indexOf('rainy')while(idx>0){weather[idx]='sunny'idx=weather.indexOf('rainy')}console.log(weather)

II. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (2) - ์‹ฌํ™”ํŽธ (Array Helper Methods)

  • ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํŠน์ • ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ธ์ž๋กœ callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด ํŠน์ง•

image-20211106153029911

II - 1. forEach

  • array.forEach(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” 3๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ
      • element: ๋ฐฐ์—ด์˜ ์š”์†Œ
      • index: ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค
      • array: ๋ฐฐ์—ด ์ž์ฒด
    • ๋ฐ˜ํ™˜ ๊ฐ’(return)์ด ์—†๋Š” ๋ฉ”์„œ๋“œ

image-20211106153701098

II - 2. map

  • array.map(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์š”์†Œ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉ

image-20211106153757715

II - 3. filter

  • array.filter(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ธ ์š”์†Œ๋“ค๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋งํ•  ๋•Œ ์œ ์šฉ

image-20211106153900239

II - 4. reduce

  • array.reduce(callback(acc, element, [index[, array]])[, intialValue])

image-20211106154012229

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๊ฐ’(acc)์— ๋ˆ„์  ํ›„ ๋ฐ˜ํ™˜
  • reduce ๋ฉ”์„œ๋“œ์˜ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜
    • acc
      • ์ด์ „ callback ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ๋ˆ„์ ๋˜๋Š” ๋ณ€์ˆ˜
    • initialValue(optional)
      • ์ตœ์ดˆ callback ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ acc์— ํ• ๋‹น ๋˜๋Š” ๊ฐ’, default ๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’

์˜ˆ์‹œ

image-20211106154132959

image-20211106154155526

II - 5. find

  • array.find(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ด๋ฉด ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    • ์ฐพ๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์— ์—†์œผ๋ฉด undefined ๋ฐ˜ํ™˜

image-20211106154318481

II - 6. some

  • array.some(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106154411525

II - 7. every

  • array.every(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ์ฐธ ๋ฐ˜ํ™˜

image-20211106154458758

์‹ค์Šต

/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 1] ์†๋ ฅ(distance/time)์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด speeds๋ฅผ ๋งŒ๋“œ์„ธ์š”.*/consttrips=[{distance: 34,time: 10},{distance: 90,time: 50},{distance: 59,time: 25},]constspeeds=trips.map((trip)=>{returntrip.distance/trip.time})console.log(speeds)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 2] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํŠน์ • ๋ฌธ์ž(query)๊ฐ€ ํฌํ•จ๋˜๋Š” ์š”์†Œ๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.*/constlanguages=['python','javascript','html','java']constquery='java'constresults=languages.filter((language)=>{returnlanguage.includes(query)})console.log(results)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 3] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ์„ธ์š”. { smith: 90, peter: 80, anna: 85, }*/constscores=[{name: 'smith',score: 90},{name: 'peter',score: 80},{name: 'anna',score: 85},]constresult=scores.reduce((res,score)=>{res[score.name]=score.scorereturnres},{})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 4] ์ฃผ์–ด์ง„ accounts ๋ฐฐ์—ด์—์„œ balance๊ฐ€ 24,000์ธ ์‚ฌ๋žŒ์„ ์ฐพ์œผ์„ธ์š”.*/constaccounts=[{name: 'justin',balance: 1200},{name: 'harry',balance: 50000},{name: 'jason',balance: 24000},]constresult=accounts.find((account)=>{returnaccount.balance===24000})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 5] ์ฃผ์–ด์ง„ requests ๋ฐฐ์—ด์—์„œ status๊ฐ€ pending์ธ ์š”์ฒญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.*/constrequests=[{url: '/photos',status: 'complete'},{url: '/albums',status: 'pending'},{url: '/users',status: 'failed'},]constresult=requests.some((req)=>{returnreq.status==='pending'})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 6] ์ฃผ์–ด์ง„ users ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ชจ๋“  ์œ ์ €์˜ ์ƒํƒœ๊ฐ€ submmited์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์„ธ์š”.*/constusers=[{username: 'eric',submitted: true},{username: 'justin',submitted: true},{username: 'james',submitted: false},]constresult=users.every((user)=>{returnuser.submitted})console.log(result)constkey='regions'constvalue=['๊ตฌ๋ฏธ','๊ด‘์ฃผ','๋Œ€์ „','์„œ์šธ','๋ถ€์šธ๊ฒฝ']constloca={[key]: value,}console.log(loca)

๊ฐ์ฒด (Objects)

๊ฐ์ฒด์˜ ์ •์˜์™€ ํŠน์ง•

  • ๊ฐ์ฒด๋Š” ์†์„ฑ(property)์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€์— key์™€ value์˜ ์Œ์œผ๋กœ ํ‘œํ˜„
  • key๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ
  • value๋Š” ๋ชจ๋“  ํƒ€์ž… ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด ์š”์†Œ ์ ‘๊ทผ์€ ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ€๋Šฅ
    • key ์ด๋ฆ„์— ๋„์–ด์“ฐ๊ธฐ ๊ฐ™์€ ๊ตฌ๋ถ„์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋Œ€๊ด€ํ˜ธ ์ ‘๊ทผ๋งŒ ๊ฐ€๋Šฅ

I. ๊ฐ์ฒด ๊ด€๋ จ ES6 ๋ฌธ๋ฒ•

I - 1. ์†์„ฑ๋ช… ์ถ•์•ฝ (shorthand)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์™€ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถ•์•ฝ ๊ฐ€๋Šฅ

image-20211106154839156

I - 2. ๋ฉ”์„œ๋“œ๋ช… ์ถ•์•ฝ(shorthand)

  • ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ function ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ

image-20211106160105850

I - 3. ๊ณ„์‚ฐ๋œ ์†์„ฑ(computed property name)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์˜ ์ด๋ฆ„์„ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ

image-20211106160915728

I - 4. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (destructing assignment)

  • ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์†์„ฑ์„ ๋ณ€์ˆ˜์— ์‰ฝ๊ฒŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•

image-20211106161339931

์‹ค์Šต

/* [Object ์š”์†Œ ์ ‘๊ทผ ์—ฐ์Šต]	์ฃผ์–ด์ง„ ๊ฐ์ฒด์—์„œ items์˜ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ์˜ name์„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์„ธ์š”.*/constdata={id: 42,items: [{id: 1,name: 'foo',},{id: 2,name: 'bar',},],}constresult=data.items[0].nameconsole.log(result)/*[Object ์ถ•์•ฝ ๋ฌธ๋ฒ•]์•„๋ž˜ ๋ณ€์ˆ˜๋“ค์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๋Š” Object๋ฅผ ์ถ•์•ฝ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.*/constusername='hailey'constcontact='010-1234-5678'constresult={
username,
contact
}console.log(result)/*[Object Destructuring]์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ Object ์ถ•์•ฝ ๋ฌธ๋ฒ•๊ณผ, destructuring์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ž‘์„ฑํ•˜์„ธ์š”.*/constusers=[{name: 'hailey',contact: '010-1234-5678',},{name: 'paul',contact: '010-5678-1234',},]// ๋ณ€๊ฒฝ ํ›„functionsaveUserData(users){// Destructuring// const { name, contact } = userconstuserData=users.map(({ name, contact },id)=>{// ์ถ•์•ฝ ๋ฌธ๋ฒ• ํ‚ค์™€ ๋ฒจ๋ฅ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ํ•˜๋‚˜๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ!return{id, name, contact}})returnuserData}// ๋ณ€๊ฒฝ ์ „functionsaveUserData(users){constuserData=users.map((user,index)=>{return{id: index,name: user.name,contact: user.contact}})returnuserData}

II. JSON (JavaScript Object Notation)

  • key-value์Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ์–ธ์–ด ๋…๋ฆฝ์  ํ‘œ์ค€ ํฌ๋งท
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฒผ์œผ๋‚˜ ์‹ค์ œ๋กœ๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…
    • ๋”ฐ๋ผ์„œ JS์˜ ๊ฐ์ฒด๋กœ์จ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ตฌ๋ฌธ ๋ถ„์„(parsing)์ด ํ•„์ˆ˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” JSON์„ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ ์ œ๊ณต
    • JSON.parse()
      • JSON => ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
    • JSON.stringify()
      • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด => JSON

์˜ˆ์‹œ

image-20211106161640662

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Latest commit

History

History
514 lines (329 loc) ยท 12.2 KB

File metadata and controls

514 lines (329 loc) ยท 12.2 KB

๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด

๋ฐฐ์—ด(Arrays)

๋ฐฐ์—ด์˜ ์ •์˜์™€ ํŠน์ง•

  • ํ‚ค์™€ ์†์„ฑ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋Š” ์ฐธ์กฐ ํƒ€์ž…์˜ ๊ฐ์ฒด(object)
  • ์ˆœ์„œ๋ฅผ ๋ณด์žฅํ•˜๋Š” ํŠน์ง•์ด ์žˆ์Œ
  • ์ฃผ๋กœ ๋Œ€๊ด„ํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•˜๊ณ , 0์„ ํฌํ•จํ•œ ์–‘์˜ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋กœ ํŠน์ • ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” array.length ํ˜•ํƒœ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๋Š” array.length - 1 ๋กœ ์ ‘๊ทผ

image-20211106151517307

I. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (1) - ๊ธฐ๋ณธํŽธ (๊ธฐ๋ณธ ๋ฐฐ์—ด ์กฐ์ž‘)

image-20211106151555718

I - 1. reverse

  • array.reverse()
    • ์›๋ณธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์˜ ์ˆœ์„œ๋ฅผ ๋ฐ˜๋Œ€๋กœ ์ •๋ ฌ

image-20211106151649779

I - 2. push & pop

  • array.push()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
  • array.pop()
    • ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152033152

I - 3. unshift & shift

  • array.unshift()
    • ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
  • array.shift()
    • ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ œ๊ฑฐ

image-20211106152115109

I - 4. includes

  • array.includes(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํŒ๋ณ„ ํ›„ ์ฐธ ๋˜๋Š” ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106152159364

I - 5. indexOf

  • array.indexOf(value)
    • ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ ํ›„ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ๋กœ ์ฐพ์€ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
    • ๋งŒ์•ฝ ํ•ด๋‹น ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ -1 ๋ฐ˜ํ™˜

image-20211106152620202

I - 6. join

  • array.join([separator])
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ๋ฐ˜ํ™˜
    • separator(๊ตฌ๋ถ„์ž)๋Š” ์„ ํƒ์ ์œผ๋กœ ์ง€์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์ƒ๋žต ์‹œ ์‰ผํ‘œ๋ฅผ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉ

image-20211106152753676

์‹ค์Šต

/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 1]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ null ๊ฐ’์„ ์ œ๊ฑฐํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ์„ธ์š”.*/consthomeworks=['david.zip',null,'maria.zip','tom.zip',null]constresult=[]for(lethomeworkofhomeworks){if(homework){result.push(homework)}}console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 2]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์•„๋ž˜ ๋ฌธ์ž์—ด์„ ์™„์„ฑํ•˜์„ธ์š”.	'www.samsung.com/sec/buds/galaxy-buds-pro'*/constarr1=['www','samsung','com']constarr2=['galaxy','buds','pro']constarr3=['sec','buds']constmainURL=arr1.join('.')constmodel=arr2.join('-')arr3.unshift(mainURL)arr3.push(model)constresult=arr3.join('/')console.log(result)/*	[๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต 3]	์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ๋ชจ๋“  'rainy' ์š”์†Œ๋ฅผ 'sunny'๋กœ ๊ต์ฒดํ•˜์„ธ์š”	- indexOf ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.*/constweather=['sunny','sunny','sunny','sunny','rainy','rainy','sunny']for(lettempofweather){if(temp=='rainy'){letidx=weather.indexOf('rainy')weather[idx]='sunny'}}letidx=weather.indexOf('rainy')while(idx>0){weather[idx]='sunny'idx=weather.indexOf('rainy')}console.log(weather)

II. ๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ๋ชฉ๋ก (2) - ์‹ฌํ™”ํŽธ (Array Helper Methods)

  • ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํŠน์ • ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ ์ธ์ž๋กœ callback ํ•จ์ˆ˜๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์ด ํŠน์ง•

image-20211106153029911

II - 1. forEach

  • array.forEach(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” 3๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ตฌ์„ฑ
      • element: ๋ฐฐ์—ด์˜ ์š”์†Œ
      • index: ๋ฐฐ์—ด ์š”์†Œ์˜ ์ธ๋ฑ์Šค
      • array: ๋ฐฐ์—ด ์ž์ฒด
    • ๋ฐ˜ํ™˜ ๊ฐ’(return)์ด ์—†๋Š” ๋ฉ”์„œ๋“œ

image-20211106153701098

II - 2. map

  • array.map(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์š”์†Œ๋กœ ํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด ์ „์ฒด๋ฅผ ๋‹ค๋ฅธ ํ˜•ํƒœ๋กœ ๋ฐ”๊ฟ€ ๋•Œ ์œ ์šฉ

image-20211106153757715

II - 3. filter

  • array.filter(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ธ ์š”์†Œ๋“ค๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
    • ๊ธฐ์กด ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋งํ•  ๋•Œ ์œ ์šฉ

image-20211106153900239

II - 4. reduce

  • array.reduce(callback(acc, element, [index[, array]])[, intialValue])

image-20211106154012229

  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๊ฐ’(acc)์— ๋ˆ„์  ํ›„ ๋ฐ˜ํ™˜
  • reduce ๋ฉ”์„œ๋“œ์˜ ์ฃผ์š” ๋งค๊ฐœ๋ณ€์ˆ˜
    • acc
      • ์ด์ „ callback ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ๋ˆ„์ ๋˜๋Š” ๋ณ€์ˆ˜
    • initialValue(optional)
      • ์ตœ์ดˆ callback ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ acc์— ํ• ๋‹น ๋˜๋Š” ๊ฐ’, default ๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ’

์˜ˆ์‹œ

image-20211106154132959

image-20211106154155526

II - 5. find

  • array.find(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์ฐธ์ด๋ฉด ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜
    • ์ฐพ๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์— ์—†์œผ๋ฉด undefined ๋ฐ˜ํ™˜

image-20211106154318481

II - 6. some

  • array.some(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ๊ฑฐ์ง“ ๋ฐ˜ํ™˜

image-20211106154411525

II - 7. every

  • array.every(callback(element[, index[, array]]))
    • ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜๋ฅผ ํ†ต๊ณผํ•˜๋ฉด ์ฐธ์„ ๋ฐ˜ํ™˜
    • ๋ชจ๋“  ์š”์†Œ๊ฐ€ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๊ฑฐ์ง“ ๋ฐ˜ํ™˜
    • ๋นˆ ๋ฐฐ์—ด์€ ํ•ญ์ƒ ์ฐธ ๋ฐ˜ํ™˜

image-20211106154458758

์‹ค์Šต

/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 1] ์†๋ ฅ(distance/time)์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด speeds๋ฅผ ๋งŒ๋“œ์„ธ์š”.*/consttrips=[{distance: 34,time: 10},{distance: 90,time: 50},{distance: 59,time: 25},]constspeeds=trips.map((trip)=>{returntrip.distance/trip.time})console.log(speeds)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 2] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘ ํŠน์ • ๋ฌธ์ž(query)๊ฐ€ ํฌํ•จ๋˜๋Š” ์š”์†Œ๋งŒ ๋ชจ์•„์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.*/constlanguages=['python','javascript','html','java']constquery='java'constresults=languages.filter((language)=>{returnlanguage.includes(query)})console.log(results)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 3] ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ์„ธ์š”. { smith: 90, peter: 80, anna: 85, }*/constscores=[{name: 'smith',score: 90},{name: 'peter',score: 80},{name: 'anna',score: 85},]constresult=scores.reduce((res,score)=>{res[score.name]=score.scorereturnres},{})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 4] ์ฃผ์–ด์ง„ accounts ๋ฐฐ์—ด์—์„œ balance๊ฐ€ 24,000์ธ ์‚ฌ๋žŒ์„ ์ฐพ์œผ์„ธ์š”.*/constaccounts=[{name: 'justin',balance: 1200},{name: 'harry',balance: 50000},{name: 'jason',balance: 24000},]constresult=accounts.find((account)=>{returnaccount.balance===24000})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 5] ์ฃผ์–ด์ง„ requests ๋ฐฐ์—ด์—์„œ status๊ฐ€ pending์ธ ์š”์ฒญ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜์„ธ์š”.*/constrequests=[{url: '/photos',status: 'complete'},{url: '/albums',status: 'pending'},{url: '/users',status: 'failed'},]constresult=requests.some((req)=>{returnreq.status==='pending'})console.log(result)/* [๋ฐฐ์—ด ๊ด€๋ จ ์ฃผ์š” ๋ฉ”์„œ๋“œ ์—ฐ์Šต ์‹ฌํ™” 6] ์ฃผ์–ด์ง„ users ๋ฐฐ์—ด์„ ํ†ตํ•ด ๋ชจ๋“  ์œ ์ €์˜ ์ƒํƒœ๊ฐ€ submmited์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์„ธ์š”.*/constusers=[{username: 'eric',submitted: true},{username: 'justin',submitted: true},{username: 'james',submitted: false},]constresult=users.every((user)=>{returnuser.submitted})console.log(result)constkey='regions'constvalue=['๊ตฌ๋ฏธ','๊ด‘์ฃผ','๋Œ€์ „','์„œ์šธ','๋ถ€์šธ๊ฒฝ']constloca={[key]: value,}console.log(loca)

๊ฐ์ฒด (Objects)

๊ฐ์ฒด์˜ ์ •์˜์™€ ํŠน์ง•

  • ๊ฐ์ฒด๋Š” ์†์„ฑ(property)์˜ ์ง‘ํ•ฉ์ด๋ฉฐ ์ค‘๊ด„ํ˜ธ ๋‚ด๋ถ€์— key์™€ value์˜ ์Œ์œผ๋กœ ํ‘œํ˜„
  • key๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…๋งŒ ๊ฐ€๋Šฅ
  • value๋Š” ๋ชจ๋“  ํƒ€์ž… ๊ฐ€๋Šฅ
  • ๊ฐ์ฒด ์š”์†Œ ์ ‘๊ทผ์€ ์  ๋˜๋Š” ๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ€๋Šฅ
    • key ์ด๋ฆ„์— ๋„์–ด์“ฐ๊ธฐ ๊ฐ™์€ ๊ตฌ๋ถ„์ž๊ฐ€ ์žˆ์œผ๋ฉด ๋Œ€๊ด€ํ˜ธ ์ ‘๊ทผ๋งŒ ๊ฐ€๋Šฅ

I. ๊ฐ์ฒด ๊ด€๋ จ ES6 ๋ฌธ๋ฒ•

I - 1. ์†์„ฑ๋ช… ์ถ•์•ฝ (shorthand)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์™€ ํ• ๋‹นํ•˜๋Š” ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ์˜ˆ์‹œ์™€ ๊ฐ™์ด ์ถ•์•ฝ ๊ฐ€๋Šฅ

image-20211106154839156

I - 2. ๋ฉ”์„œ๋“œ๋ช… ์ถ•์•ฝ(shorthand)

  • ๋ฉ”์„œ๋“œ ์„ ์–ธ ์‹œ function ํ‚ค์›Œ๋“œ ์ƒ๋žต ๊ฐ€๋Šฅ

image-20211106160105850

I - 3. ๊ณ„์‚ฐ๋œ ์†์„ฑ(computed property name)

  • ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•  ๋•Œ key์˜ ์ด๋ฆ„์„ ํ‘œํ˜„์‹์„ ์ด์šฉํ•˜์—ฌ ๋™์ ์œผ๋กœ ์ƒ์„ฑ ๊ฐ€๋Šฅ

image-20211106160915728

I - 4. ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹น (destructing assignment)

  • ๋ฐฐ์—ด ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๋ถ„ํ•ดํ•˜์—ฌ ์†์„ฑ์„ ๋ณ€์ˆ˜์— ์‰ฝ๊ฒŒ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ๋ฒ•

image-20211106161339931

์‹ค์Šต

/* [Object ์š”์†Œ ์ ‘๊ทผ ์—ฐ์Šต]	์ฃผ์–ด์ง„ ๊ฐ์ฒด์—์„œ items์˜ ์ฒซ๋ฒˆ์งธ ์•„์ดํ…œ์˜ name์„ result ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์„ธ์š”.*/constdata={id: 42,items: [{id: 1,name: 'foo',},{id: 2,name: 'bar',},],}constresult=data.items[0].nameconsole.log(result)/*[Object ์ถ•์•ฝ ๋ฌธ๋ฒ•]์•„๋ž˜ ๋ณ€์ˆ˜๋“ค์„ ์†์„ฑ์œผ๋กœ ๊ฐ€์ง€๋Š” Object๋ฅผ ์ถ•์•ฝ๋ฌธ๋ฒ•์„ ํ™œ์šฉํ•˜์—ฌ ์ž‘์„ฑํ•˜์„ธ์š”.*/constusername='hailey'constcontact='010-1234-5678'constresult={
username,
contact
}console.log(result)/*[Object Destructuring]์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ Object ์ถ•์•ฝ ๋ฌธ๋ฒ•๊ณผ, destructuring์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ž‘์„ฑํ•˜์„ธ์š”.*/constusers=[{name: 'hailey',contact: '010-1234-5678',},{name: 'paul',contact: '010-5678-1234',},]// ๋ณ€๊ฒฝ ํ›„functionsaveUserData(users){// Destructuring// const { name, contact } = userconstuserData=users.map(({ name, contact },id)=>{// ์ถ•์•ฝ ๋ฌธ๋ฒ• ํ‚ค์™€ ๋ฒจ๋ฅ˜์˜ ์ด๋ฆ„์ด ๊ฐ™์œผ๋ฉด ํ•˜๋‚˜๋กœ ์ž‘์„ฑ ๊ฐ€๋Šฅ!return{id, name, contact}})returnuserData}// ๋ณ€๊ฒฝ ์ „functionsaveUserData(users){constuserData=users.map((user,index)=>{return{id: index,name: user.name,contact: user.contact}})returnuserData}

II. JSON (JavaScript Object Notation)

  • key-value์Œ์˜ ํ˜•ํƒœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ํ‘œ๊ธฐํ•˜๋Š” ์–ธ์–ด ๋…๋ฆฝ์  ํ‘œ์ค€ ํฌ๋งท
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์ƒ๊ฒผ์œผ๋‚˜ ์‹ค์ œ๋กœ๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…
    • ๋”ฐ๋ผ์„œ JS์˜ ๊ฐ์ฒด๋กœ์จ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ตฌ๋ฌธ ๋ถ„์„(parsing)์ด ํ•„์ˆ˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” JSON์„ ์กฐ์ž‘ํ•˜๊ธฐ ์œ„ํ•œ ๋‘ ๊ฐ€์ง€ ๋‚ด์žฅ ๋ฉ”์„œ๋“œ ์ œ๊ณต
    • JSON.parse()
      • JSON => ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด
    • JSON.stringify()
      • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด => JSON

์˜ˆ์‹œ

image-20211106161640662