๋ฐฐ์—ด

๋ฆฌ์ถฉ๋…•ยท2023๋…„ 8์›” 15์ผ
1

Javascript

๋ชฉ๋ก ๋ณด๊ธฐ
7/50

๐Ÿ“– ๋ฐฐ์—ด

  • ํ•œ๊ฐœ ์ด์ƒ์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ๋ฐ์ดํ„ฐ ํƒ€์ž…
  • ๋ฐฐ์—ด ์š”์†Œ์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ž๋ฃŒํ˜•์ด ์„ž์ผ ์ˆ˜ ์žˆ๋‹ค.

๋ฐฐ์—ด ์„ ์–ธ

let arr = new Array();
let arr = [];

์š”์†Œ ์ถ”๊ฐ€

let arr = ['lee', 'hong', 'kim'];

๊ฐ ๋ฐฐ์—ด ์š”์†Œ๋Š” 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค๊ฐ€ ๋ฐฐ์ •๋˜์–ด ์žˆ์œผ๋ฉฐ, ๊ฐ ์ธ๋ฑ์Šค๋Š” ๋ฐฐ์—ด ๋‚ด ์ˆœ์„œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
๋ฐฐ์—ด ๋‚ด ํŠน์ • ์ธ๋ฑ์Šค์˜ ๊ฐ’์„ ์–ป๊ณ  ์‹ถ์„ ๊ฒฝ์šฐ, ๋ฐฐ์—ด์„ ๋‚˜ํƒ€๋‚ด๋Š” []์•ˆ์— ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ์ž…ํ•˜๋ฉด ๋œ๋‹ค.

console.log(arr[0]);	// lee
console.log(arr[1]);	// hong
console.log(arr[2]);	// kim

์ธ๋ฑ์Šค๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์š”์†Œ ์ˆ˜์ • ๋ฐ ์ถ”๊ฐ€๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

arr[1] = 'choi';		// 1๋ฒˆ ์ธ๋ฑ์Šค choi๋กœ ๋ณ€๊ฒฝ
arr[3] = 'koo';			// 3๋ฒˆ ์ธ๋ฑ์Šค์— koo ์ถ”๊ฐ€
console.log(arr);		// [ 'lee', 'bak', 'kim', 'goo' ]

length๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐฐ์—ด ๋‚ด ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

let arr = ['lee', 'hong', 'kim'];
console.log(arr.length);		// 3

๋ฐฐ์—ด ๋ฉ”์„œ๋“œ

  • push : ๋ฐฐ์—ด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ์— ์š”์†Œ ์ถ”๊ฐ€
let arr = ['lee', 'hong', 'kim'];
arr.push('koo');
console.log(arr);		// ['lee', 'hong', 'kim', 'koo']
  • pop : ๋ฐฐ์—ด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ์ œ๊ฑฐํ•œ ์š”์†Œ ๋ฐ˜ํ™˜
let arr = ['lee', 'hong', 'kim'];
arr.pop();
console.log(arr);		// ['lee', 'hong']
  • shift : ๋ฐฐ์—ด ๊ฐ€์žฅ ์•ž ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ์ œ๊ฑฐํ•œ ์š”์†Œ ๋ฐ˜ํ™˜
let arr = ['lee', 'hong', 'kim'];
arr.shift();
console.log(arr);		// ['hong', 'kim']
  • unshift : ๋ฐฐ์—ด ๊ฐ€์žฅ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
let arr = ['lee', 'hong', 'kim'];
arr.unshift('koo');
console.log(arr);		// ['koo', 'lee', 'hong', 'kim']
  • concat : ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฐฐ์—ด ์—ฐ๊ฒฐ
let arr1 = ['foo','bar'];
let arr2 = ['baz','duck'];

let result = arr1.concat(arr2);
console.log(result);
  • sort : ๋ฌธ์ž์—ด ๋ฐฐ์—ด์ผ ๊ฒฝ์šฐ ์•ŒํŒŒ๋ฒณ์ˆœ, ์ˆซ์ž์ผ ๊ฒฝ์šฐ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
let alp = ['ccccc','aaaa','dddd', 'bbbb'];
let num = [1,5,3,9,7];

console.log(alp.sort());		[ 'aaaa', 'bbbb', 'ccccc', 'dddd' ]
console.log(num.sort());		[ 1, 3, 5, 7, 9 ]

์ฐธ๊ณ ์‚ฌ์ดํŠธ
ko.javascript

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