[JavaScript] ๋ฐฐ์—ด(Array)

๋””๋”ฉยท2022๋…„ 11์›” 2์ผ
0

Section

๋ชฉ๋ก ๋ณด๊ธฐ
12/18
post-thumbnail

๐Ÿ•– DAY +11 / 2022. 11. 03

SEB FE / Section1 / Unit9 / JavaScript ๋ฐฐ์—ด, ๊ฐ์ฒด



๐Ÿ“Œ Today I Learned

  • ๋ฐฐ์—ด(Array)

๐Ÿ’ฌ ๋ฐฐ์—ด์ด๋ž€?

๋‹ค๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…์ธ ๋ฐฐ์—ด(Array). ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.

  • ์ˆœ์„œ๋Š” ์ธ๋ฑ์Šค(index)๋ผ๊ณ  ๋ถ€๋ฅด๋ฉฐ, 1์ด ์•„๋‹Œ 0๋ถ€ํ„ฐ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ธด๋‹ค


73์€ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค ๊ฐ’, 96์€ 4(์‚ฌ)๋ฒˆ์งธ ์ธ๋ฑ์Šค ๊ฐ’

  • ๊ฐ’์€ ์š”์†Œ(element)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.


๋ฐฐ์—ด์„ ์–ด๋–ป๊ฒŒ ์ •์˜ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š”๊ฐ€?

๋Œ€๊ด„ํ˜ธ(square bracket)๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๊ฐ๊ฐ์˜ ์›์†Œ(element)๋Š” ์‰ผํ‘œ(comma)๋กœ ๊ตฌ๋ถ„์„ ํ•ด ์ค€๋‹ค.

let myNumber = [73, 98, 86, 61, 96];
  • ์šฉ์–ด๋ฅผ ์ž˜ ๊ธฐ์–ตํ•ด์•ผ ํ•œ๋‹ค.
// "`myNumber`๋ผ๋Š” ๋ฐฐ์—ด์˜ 3๋ฒˆ์งธ ์ธ๋ฑ์Šค"๋ฅผ ์กฐํšŒํ•˜๋ ค๋ฉด
myNumber[3]; 
// expected output: 61
  • ๊ฐ’์€ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•ด ์ ‘๊ทผํ•œ๋‹ค.
myNumber[3] = 200; 
// myNumber 3๋ฒˆ์งธ ๊ฐ’์„ 300์œผ๋กœ ํ• ๋‹นํ•ด ์ค€๋‹ค
myNumber;
// expected output: [73, 98, 86, 200, 96];

๐Ÿ“‚ ์˜ˆ์ œ1

let fruits = [
	'banana',
    'apple',
    'pineapple'
];
  • fruits์˜ 2๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ’์€?
    // [0] 'banana', [1] 'apple', [2] 'pineapple'
    ๋‹ต์€ 'pineapple'

  • fruits์˜ 3๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ’์€?
    // (2๋ฒˆ์งธ ๊ฐ’ 'pineapple' ๋‹ค์Œ์œผ๋กœ ์ฃผ์–ด์ง„ ๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์—) ์ธ๋ฑ์Šค๋ฅผ ๋ฒ—์–ด๋‚˜๋ฉด undefined๊ฐ€ ๋‚˜์˜จ๋‹ค.
    ๋‹ต์€ undefined


๐Ÿ“‚ ์˜ˆ์ œ2

let myNumber = [[13, 30], [73, 8], [44, 17]]
  • myNumber์˜ 1๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๊ฐ’์€?
myNumber;
[Array(2), Array(2), Array(2)]

0: [13, 30]
1: [73, 8]
2: [44, 17]
// expected output: [73, 8]
  • myNumber์˜ 1๋ฒˆ์งธ ์ธ๋ฑ์Šค๊ฐ’์˜ 0๋ฒˆ์งธ ์ธ๋ฑ์Šค์˜ ๊ฐ’์€?
// ๋ฐฐ์—ด ์•ˆ์— ๋ฐฐ์—ด์ด ์žˆ๋‹ค.
myNumber[1][0]
// expected output: 73

2์ฐจ์› ๋ฐฐ์—ด์ด๋ผ ํ•œ๋‹ค. array ์•ˆ์ชฝ์œผ๋กœ ์ค‘์ฒฉ์„ ์‹œ์ผœ์„œ 3์ฐจ์› ๋ฐฐ์—ด ๋“ฑ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.



๐Ÿ’ฌ ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ

๐Ÿ“‚ ๋ฐฐ์—ด์˜ ๊ธธ์ด

// myNumber๋ผ๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์•Œ์•„๋‚ด๋ ค๋ฉด
myNumber.length;
// expected output: 4

  • ์˜จ์ (dot)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณ€์ˆ˜์˜ ์†์„ฑ(property)์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์˜จ์ (dot)์„ ์ด์šฉํ•ด์„œ ๊ด€๋ จ๋œ ๋ช…๋ น(method)๋„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
    - ๋ช…๋ น์„ ์‹คํ–‰ํ•  ๋•Œ๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋“ฏ ์†Œ๊ด„ํ˜ธ()๋ฅผ ์—ด๊ณ  ๋Š” ํ˜•ํƒœ๋กœ ์‹คํ–‰ํ•œ๋‹ค.

๐Ÿ“‚ ๋ฐฐ์—ด ๊ฐ’ ์ถ”๊ฐ€

let myNumber = [73, 98, 86, 61];

// myNumber๋ผ๋Š” ๋ฐฐ์—ด ๋์— 96์ด๋ผ๋Š” ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด (.push ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•œ๋‹ค)
myNumber.push(96);

myNumber;
// expected output: [73, 98, 86, 61, 96];

๐Ÿ“‚ ๋ฐฐ์—ด ๋งˆ์ง€๋ง‰ ๊ฐ’ ์‚ญ์ œ

let myNumber = [73, 98, 86, 61];
// myNumber๋ผ๋Š” ๋ฐฐ์—ด ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์‚ญ์ œํ•˜๋ ค๋ฉด (.pop ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•œ๋‹ค)
myNumber.pop();

myNumber;
// expected output: [73, 98, 86];



๐Ÿ’ฌ ๋ฐฐ์—ด์˜ ๋ฐ˜๋ณต

let muNum = [73, 98, 86, 61];

"๋ฐฐ์—ด myNum์˜ n๋ฒˆ์งธ ์ธ๋ฑ์Šค"๋ฅผ ์ถœ๋ ฅํ•˜์ž console.log(myNum[n])

์กฐ๊ฑด:
1. ์ˆซ์ž(n)์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค let n = 0;
2. ์ˆซ์ž(n)๋ฅผ ์–ธ์ œ๊นŒ์ง€ ๋ฐ˜๋ณตํ• ๊นŒ?
ใ„ด ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ณด๋‹ค ์ž‘์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค n < myNum.length;
3. ์ˆซ์ž(n)๋Š” 1์”ฉ ์ฆ๊ฐ€ํ•œ๋‹ค n++; (์ฆ๊ฐ ์—ฐ์‚ฐ์ž)

for (let n = 0; n < myNum.length; n++;) {
	console.log(myNum[n]); // ๋ฐ˜๋ณตํ•  ๋‚ด์šฉ์„ ๋จผ์ € ๋„ฃ์–ด ์ค€๋‹ค.
}

๐Ÿ“‚ ์˜ˆ์ œ1

  • myNum์˜ ๋ชจ๋“  element๋ฅผ ๋ˆ„์ ํ•ด์„œ ๋”ํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์กฐ๊ฑด๊ณผ, ๋ฐ˜๋ณตํ•  ๊ตฌ๋ฌธ์€?
let myNum = [10, 20, 40, 10];
let sum = 0;

for (let n = 0; n < myNum.length; n++) {
	sum = sum + myNum[n]
	// ๋ฐ˜๋ณตํ•  ๊ตฌ๋ฌธ
}

console.log(sum);
// expected output: 80

sum์— ํ• ๋‹น์„ ํ•ด ์ฃผ์ง€ ์•Š์œผ๋ฉด undefined๊ฐ€ ๋˜์–ด undefined + 10 = NaN์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๊ฐ’์„ ์žฌํ• ๋‹น ํ•  ์ˆ˜ ์žˆ๋„๋ก sum์— 0์„ ์ดˆ๊ธฐํ™”ํ•ด ๋‘ฌ์•ผ ํ•œ๋‹ค.



๐Ÿ’ฌ ๊ฐœ๋…ํ•™์Šต1

๐Ÿ“‚ Array.isArray

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํŠน์ • ๊ฐ’์ด ๋ฐฐ์—ด์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” Array.isArray

let words = ['ํ”ผ', '๋•€', '๋ˆˆ๋ฌผ']]

typeof '๋ฌธ์ž์—ด'
// expected output: "string"

typeof 123
// expected output: "number"

typeof words
// expected output: "object"

typeof [1,2,3]
// expected output: "object"

let obj = { a: 1}

typeof obj;
// expected output: "object"

์—ฌ๊ธฐ์„œ ๋ฌด์—‡์ด Array์ธ์ง€ ๊ฐ์ฒด์ธ์ง€ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์—†๊ธฐ์— ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์จ์•ผ ํ•œ๋‹ค.
๊ทธ๋ž˜์„œ Array.isArray๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

Array.isArray('๋ฌธ์ž์—ด')
// expected output: false

Array.isArray(123)
// expected output: false

Array.isArray(words)
// expected output: ture

Array.isArray([1,2,3])
// expected output: ture
// Array.isArray์— ์ง์ ‘ ์ž…๋ ฅํ•˜์—ฌ๋„ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

Array.isArray([])
// expected output: ture
// ์—ด์ด ๋น„์–ด๋„ ๋ฐฐ์—ด์ด ๋“ค์–ด๊ฐ€๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

input์€ ๋‚ด๊ฐ€ ๊ฒ€์‚ฌํ•˜๊ณ  ์‹ถ์€ ๊ฐ์ฒด๋ช…์„ ์ž‘์„ฑํ•˜๋ฉด ๋˜๊ณ , ๊ฒฐ๊ณผ๋Š” ture / false ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ๋‚˜์˜จ๋‹ค.



๐Ÿ’ฌ ๊ฐœ๋…ํ•™์Šต2

let arr = ['code'], [states'];

// ์ฝ˜์†”๋กœ๊ทธ๋กœ๋„ ๋ณผ ์ˆ˜ ์žˆ์ง€๋งŒ
// ํ…Œ์ด๋ธ”๋กœ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
// index์™€ Value๊ฐ€ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค
console.table(arr)

๐Ÿ“‚ push

arr.push('pre')
  • .push๋Š” ๊ธฐ์กด ๋ฐฐ์—ด(Array)์—๋‹ค๊ฐ€ ์š”์†Œ(element)๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ช…๋ น(method)์ด๋‹ค.

๐Ÿ“‚ pop

arr.pop()
// expected output: "pre"

console.table(arr)
  • .pop๋Š” ๊ธฐ์กด ๋ฐฐ์—ด(Array)์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ(element)๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น(method)์ด๋‹ค.

๐Ÿ“‚ shift

arr.shift()
// expected output: "code"
  • .shift๋Š” ๊ธฐ์กด ๋ฐฐ์—ด(Array)์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ(element)๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น(method)์ด๋‹ค.

๐Ÿ“‚ unshift

arr.unshift('creative')
// expected output: 2
// 2๋Š” length์ด๋‹ค

console.table(arr)
  • .unshift๋Š” ๊ธฐ์กด ๋ฐฐ์—ด(Array)์˜ ์ฒซ ๋ฒˆ์งธ์— ์š”์†Œ(element)๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ช…๋ น(method)์ด๋‹ค.

  1. ๋’ค์— element ์ถ”๊ฐ€: push
  2. ๋’ค์— element ์‚ญ์ œ: pop
  3. ์•ž์˜ element ์ถ”๊ฐ€: unshift
  4. ์•ž์˜ element ์‚ญ์ œ: shift

addToFront
addToBack
removeFromFront
removeFromBack



๐Ÿ’ฌ ๊ฐœ๋…ํ•™์Šต3

๐Ÿ“‚ indexOf

ํŠน์ •ํ•œ ๋‹จ์–ด๊ฐ€ ๋ฐฐ์—ด ์•ˆ์— ๋“ค์–ด์žˆ๋Š”์ง€ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
ํŠน์ •ํ•œ ๋‹จ์–ด๊ฐ€ ์žˆ์„ ์‹œ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์—†์„ ์‹œ -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๋Œ€/์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•œ๋‹ค.

let words = ['Radagast', 'the', 'Brown'];

words.indexOf('the')
// expected output: 1 
// ์ธ๋ฑ์Šค์˜ n๋ฒˆ์งธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

words.indexOf('Brown')
// expected output: 2

words.indexOf('Radagast')
// expected output: 0

words.indexOf('๊ทค')
// expected output: -1
// ๋ฐฐ์—ด์— ์—†๋Š” ๋‹จ์–ด๋Š” -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Array์— ํŠน์ •ํ•œ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ฐพ๊ณ ์ž ํ•  ๋•Œ ๊ฒ€์‚ฌํ•˜๋ ค๋ฉด (ํŠน์ • ์—˜๋ฆฌ๋จผํŠธ์˜ ์กด์žฌ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋ ค๋ฉด)

words.indexOf('Brown') !== -1
// expected output: true

words.indexOf('๊ทค') !== -1
// expected output: false

๐Ÿ“‚ hasElement

ํŠน์ •ํ•œ ๋ฐฐ์—ด์— ์ฐพ๊ณ ์ž ํ•˜๋Š” ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์žˆ๋Š”์ง€ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค.

hasElement(๋ฐฐ์—ด, ์ฐพ์œผ๋ ค๋Š” ์—˜๋ฆฌ๋จผํŠธ)
// expected output: true or false

function hasElement(arr, element) {
 return arr.indexOf(element) !== -1
}

hasElement(words, 'Browns')
// expected output: true

hasElement(words, '๊ทค')
// expected output: false

๐Ÿ“‚ includes

๊ธฐ๋ณธ์ ์œผ๋กœ ๋‚ด์žฅ๋ผ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋„ ์žˆ๋‹ค.
ํŠน์ •ํ•œ ๋ฐฐ์—ด์— ์ฐพ๊ณ ์ž ํ•˜๋Š” ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์žˆ๋Š”์ง€ ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค.
ํŠน์ • ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์žˆ๋Š”์ง€ ์กด์žฌ ์—ฌ๋ถ€๋งŒ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

words.includes('Brown')
// expected output: true

words.includes(words, '๊ทค')
// expected output: false



๐Ÿ‘€ ๋Œ์•„๋ณด๊ธฐ

profile
๋ฌด์‚ฌ์™€์š”... ์—ด๊ณตํ–์š”...

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