[๐ŸŒณJavascript] ํ‘œ์ค€ ๋‚ด์žฅ ๊ฐ์ฒด - array

h-a-n-aยท2023๋…„ 1์›” 17์ผ
1

๐ŸŒณjavascript

๋ชฉ๋ก ๋ณด๊ธฐ
2/17

Array

์•„๋ž˜ ์ฝ”๋“œ๋Š” ๋ชจ๋‘ console ์ฐฝ์— ์ž…๋ ฅํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.

.length

const num = [1,2,3]
num.length // 3

.at()

const arr = ["a", "b", "c"]
arr[0] // "a"
arr.at(0) //"a"

.concat()

const num = [1,2,3]
const arr = ["a", "b", "c"]
num.concat(arr) // [1, 2, 3, 'a', 'b', 'c']

.every()

๋Œ€์ƒ ๋ฐฐ์—ด์˜ ๋ชจ~~๋“  ์š”์†Œ๊ฐ€ ์ฝœ๋ฐฑ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•จ.
ํ…Œ์ŠคํŠธ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋˜๋ฉด, ์ดํ›„ ์ฝœ๋ฐฑ์€ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  false ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const num = [1,2,3]
num.every(item => item <5) // true
num.every(item => item <2) // false

.some()

๋Œ€์ƒ ๋ฐฐ์—ด์˜ ์–ด๋Š ํ•˜๋‚˜๋ผ๋„ ์ฝœ๋ฐฑ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•จ.

const num = [1,2,3]
num.some(item => item <2) // ture

.filter()

๋Œ€์ƒ "๋ฐฐ์—ด"์—์„œ ์ฝœ๋ฐฑ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋งŒ์•ฝ ์•„๋ฌด๊ฒƒ๋„ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ•˜๋ฉด ๋นˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

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๊ฐ์ฒด}]

.find()

๋Œ€์ƒ ๋ฐฐ์—ด์—์„œ ์ฝœ๋ฐฑ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const number = [27, 140, 1996, 130, 1800, 29]
number.find(item => item>140) // 1996

.findIndex()

๋Œ€์ƒ ๋ฐฐ์—ด์—์„œ ์ฝœ๋ฐฑ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const number = [27, 140, 1996, 130, 1800, 29]
number.findIndex(item => item>140) // 2

.flat()

๋Œ€์ƒ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ํ•˜์œ„ ๋ฐฐ์—ด์„ ์ง€์ •ํ•œ ๊นŠ์ด(Depth)๊นŒ์ง€ ์ด์–ด๋ถ™์ธ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๊นŠ์ด์˜ ๊ธฐ๋ณธ๊ฐ’์€ 1 ์ด๋‹ค.

const cafe = ["์•„๋ฉ”๋ฆฌ์นด๋…ธ", "์นดํ‘ธ์น˜๋…ธ", ["์นดํŽ˜๋ผ๋–ผ", "์—์Šคํ”„๋ ˆ์†Œ", ["๋ฒ„๋ธ”ํ‹ฐ",["์—์ด๋“œ"]]]
cafe.flat() // ['์•„๋ฉ”๋ฆฌ์นด๋…ธ', '์นดํ‘ธ์น˜๋…ธ', '์นดํŽ˜๋ผ๋–ผ', '์—์Šคํ”„๋ ˆ์†Œ', Array(2)]
cafe.flat(2) //['์•„๋ฉ”๋ฆฌ์นด๋…ธ', '์นดํ‘ธ์น˜๋…ธ', '์นดํŽ˜๋ผ๋–ผ', '์—์Šคํ”„๋ ˆ์†Œ', '๋ฒ„๋ธ”ํ‹ฐ', Array(1)]
cafe.flat(Infinity) // ['์•„๋ฉ”๋ฆฌ์นด๋…ธ', '์นดํ‘ธ์น˜๋…ธ', '์นดํŽ˜๋ผ๋–ผ', '์—์Šคํ”„๋ ˆ์†Œ', '๋ฒ„๋ธ”ํ‹ฐ', '์—์ด๋“œ']

.forEach()

์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

const arr = ["a", "b", "c"]
arr.forEach(item => console.log(item))
//a
//b
//c

.includes()

๋Œ€์ƒ ๋ฐฐ์—ด์ด ํŠน์ • ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

const num = [1,2,3]
num.includes(2)// true
num.includes(4)// false

.join()

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'

.map()

๋Œ€์ƒ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋งŒํผ ์ฃผ์–ด์ง„ ์ฝœ๋ฐฑ์„ ์‹คํ–‰ํ•˜๊ณ , ์ฝœ๋ฐฑ์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

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()

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()

shift ๋Š” ๋งจ ์•ž์—์„œ ์ œ๊ฑฐ, unshift๋Š” ๋งจ ์•ž์œผ๋กœ ์ถ”๊ฐ€

const num = [1,2,3]
num.shift(1)
console.log(num) // [2,3]
num.unshift(0)
console.log(num) // [0,2,3]

.reverse()

๋Œ€์ƒ ๋ฐฐ์—ด์˜ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝํ•œ๋‹ค. ์›๋ณธ ๋ฐฐ์—ด์ด ์†์ƒ๋จ.

const city = ['LA', 'new york', 'Boston']
city.reverse() //['Boston', 'new york', 'LA']
city // ['Boston', 'new york', 'LA']

.slice()

๋Œ€์ƒ ๋ฐฐ์—ด์˜ ์ผ๋ถ€๋ฅผ ์ถ”์ถœํ•ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(์ฆ‰, ์›๋ณธ์€ ์†์ƒ๋˜์ง€ ์•Š์Œ)
์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋ถ€ํ„ฐ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜ "์ง์ „"๊นŒ์ง€ ์ถ”์ถœํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋ฅผ ์ƒ๋žตํ•˜๋ฉด ๋Œ€์ƒ ๋ฐฐ์—ด์˜ ๋๊นŒ์ง€ ์ถ”์ถœํ•œ๋‹ค.

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()

๋Œ€์ƒ ๋ฐฐ์—ด์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๊ฑฐ๋‚˜ ๊ต์ฒดํ•œ๋‹ค. (์ฆ‰, ์›๋ณธ์ด ์†์ƒ๋จ)
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

profile
ํ•˜๋ฃจํ•˜๋ฃจ๊ฐ€ ์—ฐ์Šต์ด๋‹ˆ ๋‚ด์ผ์€ ๋” ๊ฐ•ํ•ด์งˆ ๊ฒ๋‹ˆ๋‹ค

0๊ฐœ์˜ ๋Œ“๊ธ€