์๋ ์ฝ๋๋ ๋ชจ๋ console ์ฐฝ์ ์ ๋ ฅํ๊ณ ์๋ค๊ณ ๊ฐ์ ํ์.
const num = [1,2,3]
num.length // 3
const arr = ["a", "b", "c"]
arr[0] // "a"
arr.at(0) //"a"
const num = [1,2,3]
const arr = ["a", "b", "c"]
num.concat(arr) // [1, 2, 3, 'a', 'b', 'c']
๋์ ๋ฐฐ์ด์ ๋ชจ~~๋ ์์๊ฐ ์ฝ๋ฐฑ ํ
์คํธ๋ฅผ ํต๊ณผํ๋์ง ํ์ธํจ.
ํ
์คํธ๊ฐ ํ๋๋ผ๋ ํต๊ณผํ์ง ๋ชปํ๊ฒ ๋๋ฉด, ์ดํ ์ฝ๋ฐฑ์ ์คํํ์ง ์๊ณ false ๋ฐํํ๋ค.
const num = [1,2,3]
num.every(item => item <5) // true
num.every(item => item <2) // false
๋์ ๋ฐฐ์ด์ ์ด๋ ํ๋๋ผ๋ ์ฝ๋ฐฑ ํ ์คํธ๋ฅผ ํต๊ณผํ๋์ง ํ์ธํจ.
const num = [1,2,3]
num.some(item => item <2) // ture
๋์ "๋ฐฐ์ด"์์ ์ฝ๋ฐฑ ํ
์คํธ๋ฅผ ํต๊ณผํ๋ ๋ชจ๋ ์์๋ก ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋ค์ด ๋ฐํํ๋ค.
๋ง์ฝ ์๋ฌด๊ฒ๋ ํ
์คํธ๋ฅผ ํต๊ณผํ์ง ๋ชปํ๋ฉด ๋น ๋ฐฐ์ด์ ๋ฐํํ๋ค.
const num = [1,2,3]
num.filter(item => item >2) // [3]
const users = [
{name: "hana", age: 27,},
{name: "tiffany", age: 25},
]
users.filter(user => user.age >26) // [{hana๊ฐ์ฒด}]
๋์ ๋ฐฐ์ด์์ ์ฝ๋ฐฑ ํ ์คํธ๋ฅผ ํต๊ณผํ๋ ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋ฐํํ๋ค.
const number = [27, 140, 1996, 130, 1800, 29]
number.find(item => item>140) // 1996
๋์ ๋ฐฐ์ด์์ ์ฝ๋ฐฑ ํ ์คํธ๋ฅผ ํต๊ณผํ๋ ์ฒซ ๋ฒ์งธ ์์์ ์ธ๋ฑ์ค ๋ฒํธ๋ฅผ ๋ฐํํ๋ค.
const number = [27, 140, 1996, 130, 1800, 29]
number.findIndex(item => item>140) // 2
๋์ ๋ฐฐ์ด์ ๋ชจ๋ ํ์ ๋ฐฐ์ด์ ์ง์ ํ ๊น์ด(Depth)๊น์ง ์ด์ด๋ถ์ธ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํํ๋ค.
๊น์ด์ ๊ธฐ๋ณธ๊ฐ์ 1 ์ด๋ค.
const cafe = ["์๋ฉ๋ฆฌ์นด๋
ธ", "์นดํธ์น๋
ธ", ["์นดํ๋ผ๋ผ", "์์คํ๋ ์", ["๋ฒ๋ธํฐ",["์์ด๋"]]]
cafe.flat() // ['์๋ฉ๋ฆฌ์นด๋
ธ', '์นดํธ์น๋
ธ', '์นดํ๋ผ๋ผ', '์์คํ๋ ์', Array(2)]
cafe.flat(2) //['์๋ฉ๋ฆฌ์นด๋
ธ', '์นดํธ์น๋
ธ', '์นดํ๋ผ๋ผ', '์์คํ๋ ์', '๋ฒ๋ธํฐ', Array(1)]
cafe.flat(Infinity) // ['์๋ฉ๋ฆฌ์นด๋
ธ', '์นดํธ์น๋
ธ', '์นดํ๋ผ๋ผ', '์์คํ๋ ์', '๋ฒ๋ธํฐ', '์์ด๋']
์ฃผ์ด์ง ํจ์๋ฅผ ๋ฐฐ์ด ์์ ๊ฐ๊ฐ์ ๋ํด ์คํํ ์ ์๊ฒ ํด์ค๋ค.
const arr = ["a", "b", "c"]
arr.forEach(item => console.log(item))
//a
//b
//c
๋์ ๋ฐฐ์ด์ด ํน์ ์์๋ฅผ ํฌํจํ๊ณ ์๋์ง ํ์ธํ๋ค.
const num = [1,2,3]
num.includes(2)// true
num.includes(4)// false
const fruits = ['Apple', 'Banana', 'Cherry']
console.log(fruits.join()) // 'Apple,Banana,Cherry'
console.log(fruits.join(', ')) // 'Apple, Banana, Cherry'
console.log(fruits.join(' * ')) // 'Apple * Banana * Cherry'
๋์ ๋ฐฐ์ด์ ๊ธธ์ด๋งํผ ์ฃผ์ด์ง ์ฝ๋ฐฑ์ ์คํํ๊ณ , ์ฝ๋ฐฑ์ ๋ฐํ๊ฐ์ ๋ชจ์ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํํ๋ค.
const num = [1,2,3]
num.map(item => item+2) // [3,4,5]
const result = ["apple", "nana"].map(item => item.length)
console.log(result) // [5,4]
pop ์ ๋งจ ๋ค์์ ์ ๊ฑฐ, push ๋ ๋งจ ๋ค๋ก ์ถ๊ฐ.
const fruits = ['Apple', 'Banana', 'Cherry']
fruits.pop('Cherry')
console.log(fruits) //['Apple', 'Banana']
fruits.push('Pear')
console.log(fruits) // ['Apple', 'Banana', 'Pear']
shift ๋ ๋งจ ์์์ ์ ๊ฑฐ, unshift๋ ๋งจ ์์ผ๋ก ์ถ๊ฐ
const num = [1,2,3]
num.shift(1)
console.log(num) // [2,3]
num.unshift(0)
console.log(num) // [0,2,3]
๋์ ๋ฐฐ์ด์ ์์๋ฅผ ๋ณ๊ฒฝํ๋ค. ์๋ณธ ๋ฐฐ์ด์ด ์์๋จ.
const city = ['LA', 'new york', 'Boston']
city.reverse() //['Boston', 'new york', 'LA']
city // ['Boston', 'new york', 'LA']
๋์ ๋ฐฐ์ด์ ์ผ๋ถ๋ฅผ ์ถ์ถํด ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํํ๋ค.(์ฆ, ์๋ณธ์ ์์๋์ง ์์)
์ฒซ ๋ฒ์งธ ์ธ์๋ถํฐ ๋ ๋ฒ์งธ ์ธ์ "์ง์ "๊น์ง ์ถ์ถํ๊ณ , ๋ ๋ฒ์งธ ์ธ์๋ฅผ ์๋ตํ๋ฉด ๋์ ๋ฐฐ์ด์ ๋๊น์ง ์ถ์ถํ๋ค.
const month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
month.slice(0,2) // ['Jan', 'Feb']
month.slice(3,5) // ['Apr', 'May']
month.slice(3) // ['Apr', 'May', 'Jun']
๋์ ๋ฐฐ์ด์ ์์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ญ์ ํ๊ฑฐ๋ ๊ต์ฒดํ๋ค. (์ฆ, ์๋ณธ์ด ์์๋จ)
splice(index, deleteCount, ๊ต์ฒด/์ถ๊ฐํ ์์) -index๋ฒ์งธ๋ถํฐ ์ญ์ ํ deleteCount๊ฐ์
const month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
month.splice(0,1) //['Feb', 'Mar', 'Apr', 'May', 'Jun']
const month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
month.splice(6,0,"July") //['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July']
const month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
month.splice(0,1,"my-birthday") //['my-birthday', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
์ฐธ๊ณ ์ฌ์ดํธ:
https://ko.javascript.info/array-methods