๋ฐฐ์ด์ ์ ์์ ํน์ง
- ํค์ ์์ฑ๋ค์ ๋ด๊ณ ์๋ ์ฐธ์กฐ ํ์ ์ ๊ฐ์ฒด(object)
- ์์๋ฅผ ๋ณด์ฅํ๋ ํน์ง์ด ์์
- ์ฃผ๋ก ๋๊ดํธ๋ฅผ ์ด์ฉํ์ฌ ์์ฑํ๊ณ , 0์ ํฌํจํ ์์ ์ ์ ์ธ๋ฑ์ค๋ก ํน์ ๊ฐ์ ์ ๊ทผ ๊ฐ๋ฅ
- ๋ฐฐ์ด์ ๊ธธ์ด๋ array.length ํํ๋ก ์ ๊ทผ ๊ฐ๋ฅ
- ๋ฐฐ์ด์ ๋ง์ง๋ง ์์๋ array.length - 1 ๋ก ์ ๊ทผ
- array.reverse()
- ์๋ณธ ๋ฐฐ์ด์ ์์๋ค์ ์์๋ฅผ ๋ฐ๋๋ก ์ ๋ ฌ
- array.push()
- ๋ฐฐ์ด์ ๊ฐ์ฅ ๋ค์ ์์ ์ถ๊ฐ
- array.pop()
- ๋ฐฐ์ด์ ๋ง์ง๋ง ์์ ์ ๊ฑฐ
- array.unshift()
- ๋ฐฐ์ด์ ๊ฐ์ฅ ์์ ์์ ์ถ๊ฐ
- array.shift()
- ๋ฐฐ์ด์ ์ฒซ๋ฒ์งธ ์์ ์ ๊ฑฐ
- array.includes(value)
- ๋ฐฐ์ด์ ํน์ ๊ฐ์ด ์กด์ฌํ๋์ง ํ๋ณ ํ ์ฐธ ๋๋ ๊ฑฐ์ง ๋ฐํ
- array.indexOf(value)
- ๋ฐฐ์ด์ ํน์ ๊ฐ์ด ์กด์ฌํ๋์ง ํ์ธ ํ ๊ฐ์ฅ ์ฒซ ๋ฒ์งธ๋ก ์ฐพ์ ์์์ ์ธ๋ฑ์ค ๋ฐํ
- ๋ง์ฝ ํด๋น ๊ฐ์ด ์์ ๊ฒฝ์ฐ -1 ๋ฐํ
- array.join([separator])
- ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ฐ๊ฒฐํ์ฌ ๋ฐํ
- separator(๊ตฌ๋ถ์)๋ ์ ํ์ ์ผ๋ก ์ง์ ๊ฐ๋ฅํ๋ฉฐ, ์๋ต ์ ์ผํ๋ฅผ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ฌ์ฉ
์ค์ต
/* [๋ฐฐ์ด ๊ด๋ จ ์ฃผ์ ๋ฉ์๋ ์ฐ์ต 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)- ๋ฐฐ์ด์ ์ํํ๋ฉฐ ํน์ ๋ก์ง์ ์ํํ๋ ๋ฉ์๋
- ๋ฉ์๋ ํธ์ถ ์ ์ธ์๋ก callback ํจ์๋ฅผ ๋ฐ๋ ๊ฒ์ด ํน์ง
- array.forEach(callback(element[, index[, array]]))
- ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฝ๋ฐฑ ํจ์๋ฅผ ํ ๋ฒ์ฉ ์คํ
- ์ฝ๋ฐฑ ํจ์๋ 3๊ฐ์ง ๋งค๊ฐ๋ณ์๋ก ๊ตฌ์ฑ
- element: ๋ฐฐ์ด์ ์์
- index: ๋ฐฐ์ด ์์์ ์ธ๋ฑ์ค
- array: ๋ฐฐ์ด ์์ฒด
- ๋ฐํ ๊ฐ(return)์ด ์๋ ๋ฉ์๋
- array.map(callback(element[, index[, array]]))
- ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฝ๋ฐฑ ํจ์๋ฅผ ํ ๋ฒ์ฉ ์คํ
- ์ฝ๋ฐฑ ํจ์์ ๋ฐํ ๊ฐ์ ์์๋ก ํ๋ ์๋ก์ด ๋ฐฐ์ด ๋ฐํ
- ๊ธฐ์กด ๋ฐฐ์ด ์ ์ฒด๋ฅผ ๋ค๋ฅธ ํํ๋ก ๋ฐ๊ฟ ๋ ์ ์ฉ
- array.filter(callback(element[, index[, array]]))
- ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฝ๋ฐฑ ํจ์๋ฅผ ํ ๋ฒ์ฉ ์คํ
- ์ฝ๋ฐฑ ํจ์์ ๋ฐํ ๊ฐ์ด ์ฐธ์ธ ์์๋ค๋ง ๋ชจ์์ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํ
- ๊ธฐ์กด ๋ฐฐ์ด์ ์์๋ค์ ํํฐ๋งํ ๋ ์ ์ฉ
- array.reduce(callback(acc, element, [index[, array]])[, intialValue])
- ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฝ๋ฐฑ ํจ์๋ฅผ ํ ๋ฒ์ฉ ์คํ
- ์ฝ๋ฐฑ ํจ์์ ๋ฐํ ๊ฐ๋ค์ ํ๋์ ๊ฐ(acc)์ ๋์ ํ ๋ฐํ
- reduce ๋ฉ์๋์ ์ฃผ์ ๋งค๊ฐ๋ณ์
- acc
- ์ด์ callback ํจ์์ ๋ฐํ ๊ฐ์ด ๋์ ๋๋ ๋ณ์
- initialValue(optional)
- ์ต์ด callback ํจ์ ํธ์ถ ์ acc์ ํ ๋น ๋๋ ๊ฐ, default ๊ฐ์ ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ๊ฐ
- acc
์์
- array.find(callback(element[, index[, array]]))
- ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํด ์ฝ๋ฐฑ ํจ์๋ฅผ ํ ๋ฒ์ฉ ์คํ
- ์ฝ๋ฐฑ ํจ์์ ๋ฐํ ๊ฐ์ด ์ฐธ์ด๋ฉด ํด๋น ์์๋ฅผ ๋ฐํ
- ์ฐพ๋ ๊ฐ์ด ๋ฐฐ์ด์ ์์ผ๋ฉด undefined ๋ฐํ
- array.some(callback(element[, index[, array]]))
- ๋ฐฐ์ด์ ์์ ์ค ํ๋๋ผ๋ ์ฃผ์ด์ง ํ๋ณ ํจ์๋ฅผ ํต๊ณผํ๋ฉด ์ฐธ์ ๋ฐํ
- ๋ชจ๋ ์์๊ฐ ํต๊ณผํ์ง ๋ชปํ๋ฉด ๊ฑฐ์ง ๋ฐํ
- ๋น ๋ฐฐ์ด์ ํญ์ ๊ฑฐ์ง ๋ฐํ
- array.every(callback(element[, index[, array]]))
- ๋ฐฐ์ด์ ๋ชจ๋ ์์๊ฐ ์ฃผ์ด์ง ํ๋ณ ํจ์๋ฅผ ํต๊ณผํ๋ฉด ์ฐธ์ ๋ฐํ
- ๋ชจ๋ ์์๊ฐ ํต๊ณผํ์ง ๋ชปํ๋ฉด ๊ฑฐ์ง ๋ฐํ
- ๋น ๋ฐฐ์ด์ ํญ์ ์ฐธ ๋ฐํ
์ค์ต
/* [๋ฐฐ์ด ๊ด๋ จ ์ฃผ์ ๋ฉ์๋ ์ฐ์ต ์ฌํ 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)๊ฐ์ฒด์ ์ ์์ ํน์ง
- ๊ฐ์ฒด๋ ์์ฑ(property)์ ์งํฉ์ด๋ฉฐ ์ค๊ดํธ ๋ด๋ถ์ key์ value์ ์์ผ๋ก ํํ
- key๋ ๋ฌธ์์ด ํ์ ๋ง ๊ฐ๋ฅ
- value๋ ๋ชจ๋ ํ์ ๊ฐ๋ฅ
- ๊ฐ์ฒด ์์ ์ ๊ทผ์ ์ ๋๋ ๋๊ดํธ๋ก ๊ฐ๋ฅ
- key ์ด๋ฆ์ ๋์ด์ฐ๊ธฐ ๊ฐ์ ๊ตฌ๋ถ์๊ฐ ์์ผ๋ฉด ๋๊ดํธ ์ ๊ทผ๋ง ๊ฐ๋ฅ
- ๊ฐ์ฒด๋ฅผ ์ ์ํ ๋ key์ ํ ๋นํ๋ ๋ณ์์ ์ด๋ฆ์ด ๊ฐ์ผ๋ฉด ์์์ ๊ฐ์ด ์ถ์ฝ ๊ฐ๋ฅ
- ๋ฉ์๋ ์ ์ธ ์ function ํค์๋ ์๋ต ๊ฐ๋ฅ
- ๊ฐ์ฒด๋ฅผ ์ ์ํ ๋ key์ ์ด๋ฆ์ ํํ์์ ์ด์ฉํ์ฌ ๋์ ์ผ๋ก ์์ฑ ๊ฐ๋ฅ
- ๋ฐฐ์ด ๋๋ ๊ฐ์ฒด๋ฅผ ๋ถํดํ์ฌ ์์ฑ์ ๋ณ์์ ์ฝ๊ฒ ํ ๋นํ ์ ์๋ ๋ฌธ๋ฒ
์ค์ต
/* [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}- key-value์์ ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ํ๊ธฐํ๋ ์ธ์ด ๋ ๋ฆฝ์ ํ์ค ํฌ๋งท
- ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ฐ์ฒด์ ์ ์ฌํ๊ฒ ์๊ฒผ์ผ๋ ์ค์ ๋ก๋ ๋ฌธ์์ด ํ์
- ๋ฐ๋ผ์ JS์ ๊ฐ์ฒด๋ก์จ ์กฐ์ํ๊ธฐ ์ํด์๋ ๊ตฌ๋ฌธ ๋ถ์(parsing)์ด ํ์
- ์๋ฐ์คํฌ๋ฆฝํธ์์๋ JSON์ ์กฐ์ํ๊ธฐ ์ํ ๋ ๊ฐ์ง ๋ด์ฅ ๋ฉ์๋ ์ ๊ณต
- JSON.parse()
- JSON => ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด
- JSON.stringify()
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด => JSON
- JSON.parse()
์์






















