๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿค– data structures & algorithms

Data Structures & Algorithms with Javascript : Arrays

2024.07.17 - [๐Ÿค– data structures & algorithms] - Data Structures & Algorithms with Javascript

 

Data Structures & Algorithms with Javascript : Intro

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Data Structures & Algorithms with Javascript๋ฅผ ์ถ”์ฒœ๋ฐ›์•„์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ๋‹ค์Œ์˜ ์ž๋ฃŒ ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๊ณ  ํ•ด๋‹น ๋‚ด์šฉ์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.์•ž์œผ๋กœ์˜ ํฌ์ŠคํŒ…์ด ๋‹ค๋ฃฐ ๋‚ด์šฉ

pyotato-dev.tistory.com

์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๊ฐ€์žฅ ์ต์ˆ™ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋ผ๋ฉด "Array"๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ชจ๋“  ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด๋Š” ์–ด๋–ค ํ˜•ํƒœ๋กœ๋“  array๋ฅผ ๋‚ด์žฅํ•˜๊ณ  ์žˆ๋‹ค.
Array๊ฐ€ built-in์ด๊ธฐ ๋•Œ๋ฌธ์— ํšจ์œจ์ ์ด๊ณ  ๋ฐ์ดํ„ฐ ์ €์žฅ ๋ชฉ์ ์— ์ข‹์€ ์„ ํƒ์œผ๋กœ ์—ฌ๊ฒจ์ง„๋‹ค.

๐Ÿ€ ์˜ค๋Š˜์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ array๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์–ด๋–ป๊ฒŒ ์–ด๋–ค ๊ฒฝ์šฐ๋“ค์— ์‚ฌ์šฉํ•  ์ง€์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

 

Array ์‚ฌ์šฉ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Array๋ฅผ ์‚ฌ์šฉํ•ด ๋ดค๋‹ค๋ฉด ์—„์ฒญ ์œ ์—ฐํ•˜๋‹ค๋Š” ๊ฑธ ๋А๊ผˆ์„ ๊ฒƒ์ด๋‹ค.

Array๋ฅผ ์ƒ์„ฑ, ์š”์†Œ๋ฅผ ์ฐพ๊ฑฐ๋‚˜ sort ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•๋“ค์— ๋Œ€ํ•ด ํ•˜๋‚˜์”ฉ ์•Œ์•„๋ณด์ž.

 

Array ์ƒ์„ฑ

// ๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ฒŒ array ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ '[ ]' ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
const emptyArray = [];

// ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ array์— ๋‚ด์žฅ๋œ length ํ”„๋กœํผํ‹ฐ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๊ธธ์ด๊ฐ€ 0 ์ธ ๋ฐฐ์—ด์ด๋ผ๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
console.log(emptyArray.length);

// ๋ฐฐ์—ด์— ์š”์†Œ๋ฅผ ๋„ฃ์–ด ๋ฐฐ์—ด์„ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.
const numbers = [1, 2, 3, 4, 5];
console.log(numbers.length); // 5

// Array constructor์™€ new ํ‚ค์›Œ๋“œ๋กœ ํ˜ธ์ถœํ•˜์—ฌ array๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

// ์ˆซ์ž 1๊ฐœ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋ฉด ํ•ด๋‹น ์ˆซ์ž๋งŒํผ์˜ ๋นˆ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.
// ์ด๋•Œ empty๋Š” ๋น„์–ด ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๊ณ , ์‹ค์ œ empty๋ผ๋Š” ๊ฐ’์ด ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๊ฑด ์•„๋‹ˆ๋‹ค.
const empty10Arr = new Array(10);
console.log(empty10Arr); // [empty × 10]


// 2๊ฐœ ์ด์ƒ์˜ ์ˆซ์ž๋ฅผ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ์ˆซ์ž๋“ค์„ ์›์†Œ๋กœ ํ•˜๋Š” ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•œ๋‹ค.
const numbers3 = new Array(1, 2, 3, 4, 5);
console.log(numbers3.length); // 5

// (์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด๋“ค ์ œ์™ธ) ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์™€๋Š” ๋‹ฌ๋ฆฌ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ array์˜ ์š”์†Œ๋“ค์€ ๋‹ค๋ฅธ ํƒ€์ž…์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
const diffTypeArr = new Array(1, "Joe", true, null);

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด Array.isArray()์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ฒจ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

const arr = [1,2,3];
const obj = {"0":1,"1":2,"2":3};
Array.isArray(arr); // true
Array.isArray(obj); // false

 

Array constructor๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” [ ] ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ํšจ์œจ์ ์ด๋ผ๊ณ  ํ•œ๋‹ค.

Array ์š”์†Œ ์‚ฝ์ž…๊ณผ ์ ‘๊ทผ

// [๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค] = ๋ฐฐ์—ด ์ธ๋ฑ์Šค์— ํ• ๋‹นํ•  ๊ฐ’ ํ˜•ํƒœ๋กœ [] ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•ด ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.
const nums = [];
for(let i = 0; i < 100; i++){
	nums[i] = i+1;
}

// ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋กœ ์š”์†Œ์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋‹ค
const nums2 = [1,2,3,5,8,13,21];
let sum = 0;
for(let i = 0; i < numbers.length; i++){
	sum += nums2[i];
}

console.log(sum); //53

String์œผ๋กœ๋ถ€ํ„ฐ ๋ฐฐ์—ด ์ƒ์„ฑ

string์„ ์ž˜๋ผ์ค„ ๊ธฐ์ค€ ๋ฌธ์ž์—ด์„ split()์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•˜์—ฌ ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

const sentence = "the quick brown fox jumped over the lazy dog"; 
const words = sentence.split(" ");
for (let i = 0; i < words.length; ++i) {
       console.log("word " + i + ": " + words[i]);
    }
    /**
    word 0: the
    word 1: quick
    word 2: brown
    word 3: fox
    word 4: jumped
    word 5: over
    word 6: the
    word 7: lazy
    word 8: dog
    */

๋ฐฐ์—ด ํ•ฉ์ณ์ฃผ๊ธฐ

์–•์€ ๋ณต์‚ฌ

// ์–•์€ ๋ณต์‚ฌ : ๋ฐฐ์—ด์€ ๊ฐ์ฒด๋‹ค. ๊ฐ์ฒด๋Š” ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์—
// ์›๋ณธ ๋ฐฐ์—ด์„ ๋‹ค๋ฅธ ๋ฐฐ์—ด์— ํ• ๋‹นํ•˜๋ฉด ๋ณต์‚ฌ๋ณธ์ธ ๋ฐฐ์—ด์€ ์›๋ณธ ๋ฐฐ์—ด์˜ ์ฃผ์†Œ๋ฅผ ์ฐธ์กฐํ•˜์—ฌ (์„œ๋กœ ๊ฐ™์€ ๊ฑธ ์ฐธ์กฐ) 
// ๋ณต์‚ฌ๋ณธ์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์›๋ณธ ๋ฐฐ์—ด์—๋„ ๋ฐ˜์˜๋œ๋‹ค (์„œ๋กœ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ๋ฐ˜์˜).
const nums = [];
for (var i = 0; i < 10; ++i) {
       nums[i] = i+1;
    }
const samenums = nums;

nums[0] = 400;
samenums[0];

 

๊นŠ์€ ๋ณต์‚ฌ

// ์–•์€ ๋ณต์‚ฌ๋ฅผ ํ•˜๋ฉด ๊ฐ’์ด ์–ด๋””์„œ ์˜๋„์น˜ ์•Š๊ฒŒ ๋ณ€๊ฒฝ๋  ์ˆ˜๋„ ์žˆ๋‹ค.
// ๊นŠ์€ ๋ณต์‚ฌ๋กœ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

// ๋ณต์‚ฌ ํ•จ์ˆ˜
function copy(arr1, arr2) {
for (let i = 0; i < arr1.length; ++i) {
          arr2[i] = arr1[i];
       }
}

// ์‚ฌ์šฉ
const nums = [];
for (let i = 0; i < 100; ++i) {
       nums[i] = i+1;
    }
const samenums = [];
copy(nums, samenums);
nums[0] = 400; 
console.log(samenums[0]); // 1

Array ์ ‘๊ทผ ํ•จ์ˆ˜

์•ž์—์„œ array์˜ ์ธ๋ฑ์Šค๋ฅผ [] ์—ฐ์‚ฐ์ž๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฑธ ์‚ดํŽด๋ดค๋‹ค.

์ด ์™ธ์— array ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์ ‘๊ทผ์ž ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ์ ‘๊ทผ์ž ํ•จ์ˆ˜๋“ค์„ ์‚ดํŽด๋ณด์ž.

 

indexOf() : array์— ์š”์†Œ๊ฐ€ ๋“ฑ์žฅํ•˜์ง€ ์•Š์œผ๋ฉด -1, ๋“ฑ์žฅํ•˜๋ฉด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋“ฑ์žฅ ์ธ๋ฑ์Šค

const names = ["David", "Cynthia", "Raymond", "Clayton", "Jennifer"]; 

const present_name = 'Cynthia';
const non_present_name = 'Elsa';

function getPosition(name){
    const position = names.indexOf(name);
    if (position >= 0) {
    	console.log("Found " + name + " at position " + position);
    }
    else {
        console.log(name + " not found in array.");
    }
}

getPosition(present_name); // Found Cynthia at position 1
getPosition(non_present_name); // Elsa not found in array.

Array๋ฅผ String ํ˜•ํƒœ๋กœ

join() : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ธด ๋ฌธ์ž์—ด๋กœ array ์š”์†Œ๋“ค์„ ํ•ฉ์ณ์ค€ string ์ƒ์„ฑ, ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋„˜๊ธฐ์ง€ ์•Š๋‹ค๋ฉด ', '๋กœ ํ•ฉ์ณ์ค€๋‹ค.

toString() : ', '๋กœ array ์š”์†Œ๋“ค์„ ํ•ฉ์ณ์ค€ string ์ƒ์„ฑ

const names = ["David", "Cynthia", "Raymond", "Clayton", "Mike", "Jennifer"];
names.join(); // 'David,Cynthia,Raymond,Clayton,Mike,Jennifer'
names.toString(); // 'David,Cynthia,Raymond,Clayton,Mike,Jennifer'

Array๋“ค๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด Array ์ƒ์„ฑ

concat() : ๋‘ ๊ฐœ ์ด์ƒ์˜ array๋ฅผ ์ด์–ด ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด. concat(์ด์–ด ๋ถ™์—ฌ์ค„ ๋ฐฐ์—ด) ํ˜•ํƒœ

const lowercase = ["a", "b", "c", "d", "e"];
const uppercase = ["A", "B", "C","D","E"];
const lower_upper = lowercase.concat(uppercase);
const upper_lower = uppercase.concat(uppercase);
console.log(lower_upper) // ['a', 'b', 'c', 'd', 'e', 'A', 'B', 'C', 'D', 'E']
console.log(upper_lower) // ['A', 'B', 'C', 'D', 'E', 'A', 'B', 'C', 'D', 'E']

 

splice() : ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ฐจ๋ก€๋Œ€๋กœ ์‹œ์ž‘ ์ธ๋ฑ์Šค, ์‚ญ์ œํ•ด ์ค„ ํ•ญ๋ชฉ ๊ฐœ์ˆ˜, ์ถ”๊ฐ€ํ•ด ์ค„ ์š”์†Œ๋“ค ํ˜•ํƒœ.

์›๋ณธ array์„ ๋ณ€๊ฒฝํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ์— ๋ณ€๊ฒฝ์—†์ด ์ƒˆ๋กœ์šด array๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด slice(์‹œ์ž‘์ธ๋ฑ์Šค, ๋ ์ธ๋ฑ์Šค)๋ฅผ ํ™œ์šฉํ•˜์ž.

 

const lowercase = ["a", "b", "c", "d", "e","f","g","h"];

// lowercase์˜ ์ธ๋ฑ์Šค 3๋ฒˆ๋ถ€ํ„ฐ 3๊ฐœ๋ฅผ ์ž˜๋ผ์„œ ๋ฐฐ์—ด์„ ์ƒ์„ฑ
const spliced = lowercase.splice(3,3);

console.log(spliced); // ["d", "e","f"]
console.log(lowercase); // ["a", "b", "c", "g", "h"]

// ์‹œ์ž‘ ์ธ๋ฑ์Šค 3์—์„œ๋ถ€ํ„ฐ 0๊ฐœ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•(...)์„ ํ™œ์šฉํ•˜์—ฌ spliced์˜ ์›์†Œ๋“ค์„ ์‚ฝ์ž…
lowercase.splice(3,0,...spliced); 
console.log(lowercase); // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

Array mutator ํ•จ์ˆ˜

push() : array ๋์— ์š”์†Œ ์ถ”๊ฐ€

const nums = [1, 2, 3, 4, 5];
console.log(nums); // 1, 2, 3, 4, 5
nums.push(6);
console.log(nums); // 1, 2, 3, 4, 5, 6

 

unshift() : array ์•ž์— ์š”์†Œ ์ถ”๊ฐ€, ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ','๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ

// ์ง์ ‘ ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ ๋ฐ€์–ด์ฃผ๊ณ  ๋งจ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€ 
const nums = [2,3,4,5];
const newnum = 1;
const N = nums.length;
for (let i = N; i >= 0; --i) {
       nums[i] = nums[i-1];
    }
nums[0] = newnum;
console.log(nums); // [1, 2, 3, 4, 5]

// unshift๋ฅผ ํ†ตํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์•ž์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค

const newnum = 1; 

let nums = [2,3,4,5]; 
print(nums); // [2,3,4,5]

nums.unshift(newnum); 
console.log(nums); // [1,2,3,4,5] 

nums = [3,4,5]; 
nums.unshift(newnum,1,2); 
console.log(nums); // [1, 1, 2, 3, 4, 5]

Array ์š”์†Œ ์ œ๊ฑฐ

pop() : array ๋งจ ๋ ์š”์†Œ ์ œ๊ฑฐ

const nums = [1,2,3,4,5];
nums.pop(); // 5
console.log(nums); // [1,2,3,4]

 

shift() : array ๋งจ ์ฒซ ์š”์†Œ ์ œ๊ฑฐ

const nums = [1,2,3,4,5];

// ์ง์ ‘ ๋งจ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
const nums = [9,1,2,3,4,5];
console.log(nums); // [9,1,2,3,4,5]
for (let i = 0; i < nums.length; ++i) {
       nums[i] = nums[i+1];
    }
console.log(nums); //  [1, 2, 3, 4, 5, undefined] // ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋Š” ๊ทธ๋Œ€๋กœ๋ผ์„œ ๋ฐ˜์ชฝ์งœ๋ฆฌ ์ œ๊ฑฐ๋‹ค

// shift() ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด ๋งจ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
nums.shift(); // 9
console.log(nums); // [1,2,3,4,5]

Array ์š”์†Œ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ

reverse() : ์š”์†Œ๋“ค์„ ๋‚ด๋ฆผ์ฐจ์ˆœ(ํฐ ๊ฐ’๋ถ€ํ„ฐ ์ž‘์€ ๊ฐ’ ์ˆœ)์œผ๋กœ ์ •๋ ฌ

const nums = [1,2,3,4,5]; 
nums.reverse(); 
console.log(nums); //  [5, 4, 3, 2, 1]

 

sort() : ์‚ฌ์ „์ ์œผ๋กœ ์š”์†Œ๋“ค์„ ์ •๋ ฌ

// sort() ํ•จ์ˆ˜๋Š” ์š”์†Œ๋“ค์ด ๋ฌธ์ž์—ด์ด๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์ •๋ ฌํ•œ๋‹ค.
// ์ฆ‰ 100์€ ์ˆซ์ž 100์ด ์•„๋‹ˆ๋ผ ๋ฌธ์ž "1","0","0"์ด ์ด์–ด์ง„ ๊ฐ’์ด๊ณ , 2๋„ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž "2"์ด๋‹ค.
// ๋”ฐ๋ผ์„œ ์ˆซ์ž ์ˆœ์„œ 1,2,3,4,100,200 ์ด ์•„๋‹ˆ๋ผ 
// ๋ฌธ์ž ์ˆœ์„œ "1", "100", "2" , "200", "3", "4"๋กœ ์ •๋ ฌํ•œ๋‹ค.
const nums = [3, 1, 2 , 100, 4, 200]; 
nums.sort(); 
console.log(nums); // [1, 100, 2, 200, 3, 4]

// sort()ํ•จ์ˆ˜์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋น„๊ต ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ˆซ์ž์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

function compare(num1, num2) { 
	return num1 - num2;
}
nums.sort(compare); 
console.log(nums); // [1, 2, 3, 4, 100, 200]

 

Iterator ํ•จ์ˆ˜

iteratorํ•จ์ˆ˜๋Š” ๋ฐฐ์—ด ์š”์†Œ๋“ค์„ ์ˆœํšŒํ•˜๋ฉด์„œ ์—ฐ์‚ฐ์„ ํ•˜๊ณ  ๋‚˜์„œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ๊ณผ, ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์žˆ๋‹ค.

๋จผ์ € ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š” iterator ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด์ž.

 

forEach() : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜์˜ ์—ฐ์‚ฐ์„ array์˜ ๊ฐ ์š”์†Œ์— ์ ์šฉ

function square(num) { 
	console.log(num, num * num);
}
const nums = [1,2,3,4,5,6,7,8,9,10];
nums.forEach(square);

/**
    1 1 
    2 4 
    3 9
    4 16
    5 25
    6 36
    7 49
    8 64
    9 81 10 100
*/

 

every() : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ boolean ํ•จ์ˆ˜๋ฅผ array์˜ ๊ฐ ์š”์†Œ์— ์ ์šฉํ•˜๊ณ  ๋ชจ๋“  ์š”์†Œ๊ฐ€ true์ธ ๊ฒฝ์šฐ true๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

function isEven(num) { 
	return num % 2 == 0;
}
const num1 = [2,4,6,8,10];
const num2 = [2,4,6,7,8,10];
const allAreEven = (nums) => nums.every(isEven)? "all numbers are even":"not all numbers are even"; 

console.log(allAreEven(num1)); // all numbers are even
console.log(allAreEven(num2)); // not all numbers are even

 

some() : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ boolean ํ•จ์ˆ˜๋ฅผ array์˜ ๊ฐ ์š”์†Œ์— ์ ์šฉํ•˜๊ณ  ํ•˜๋‚˜๋ผ๋„ ์š”์†Œ๊ฐ€ true์ธ ๊ฒฝ์šฐ true๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

function isEven(num) { 
	return num % 2 == 0;
}
const num1 = [1,3,5,7,8,9];
const num2 = [1,3,5,7,9];
const someAreEven = (nums) => nums.some(isEven)? "some numbers are even":"all numbers are not even"; 

someAreEven(num2) // 'some numbers are even'
someAreEven(num2) // 'all numbers are not even'

 

reduce() : ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•œ ํ•จ์ˆ˜์˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์ถ•์ ํ•ด์„œ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

// reduce๋Š” ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์—ฐ์‚ฐ์ด ์ง„ํ–‰๋œ๋‹ค.
function add(accumulatedValue, currentValue) { 
	return accumulatedValue + currentValue;
}
const nums = [1,2,3,4,5,6,7,8,9,10]; 
const sum = nums.reduce(add); 
console.log(sum); // 55


// ๋ฐ˜๋ฉด reduceRight()๋กœ ์˜ค๋ฅธ์ชฝ์—์„œ ์™ผ์ชฝ์œผ๋กœ ์—ฐ์‚ฐ์ด ๋˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.
function concat(accumulatedString, item) { 
	return accumulatedString + item;
}
const words = ["the ", "quick ","brown ", "fox "]; 
const sentence = words.reduceRight(concat); 
console.log(sentence); // 'fox brown quick the '

 

์ด๋ฒˆ์—๋Š” ์ƒˆ๋กœ์šด array๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋“ค์„ ์‚ดํŽด๋ณด์ž.

 

map() : forEach() ํ•จ์ˆ˜์ฒ˜๋Ÿผ array๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ์š”์†Œ์— ์—ฐ์‚ฐ์„ ํ•œ๋‹ค. forEach()์™€ ๋‹ฌ๋ฆฌ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์€ ์ƒˆ๋กœ์šด array ์ƒ์„ฑํ•˜์—ฌ ๋ฆฌํ„ด

function curve(grade) { 
	return grade += 5;
}
const grades = [77, 65, 81, 92, 83];
const newgrades = grades.map(curve); 
console.log(newgrades); // [82, 70, 86, 97, 88]

 

filter() : array๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ ์š”์†Œ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ boolean ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜์—ฌ true์ธ ์š”์†Œ ๊ฐ’๋งŒ ๋ฆฌํ„ดํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ

function isEven(num) { 
	return num % 2 == 0;
}
function isOdd(num) { 
	return num % 2 != 0;
}

const nums = [];
for (let i = 0; i < 20; ++i) {
       nums[i] = i+1;
    }

const evens = nums.filter(isEven); 
const odds = nums.filter(isOdd); 

console.log(`Even numbers: ${evens}`);  // 'Even numbers: 2,4,6,8,10,12,14,16,18,20'
console.log(`Odd numbers: ${odds}`); // 'Odd numbers: 1,3,5,7,9,11,13,15,17,19'

์ด์ฐจ์›/ ๋‹ค์ฐจ์› Array

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ array๋Š” 1์ฐจ ๋ฐฐ์—ด์ด๋‹ค. ํ•˜์ง€๋งŒ ๋ฐฐ์—ด์„ ์ค‘์ฒฉํ•˜์—ฌ ๋‹ค์ฐจ์› ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด์ฐจ์› Array ์ƒ์„ฑ

// array๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‚ด๋ถ€์— 5๊ฐœ์˜ ๋นˆ ๋ฐฐ์—ด์„ ์‚ฝ์ž…
var twod = [];
var rows = 5;
for (var i = 0; i < rows; ++i) {
       twod[i] = [];
    }

console.log(twod) // (5) [Array(0), Array(0), Array(0), Array(0), Array(0)]

// ์ง์ ‘ ์ƒ์„ฑํ•จ์ˆ˜ ๊ตฌํ˜„

Array.matrix = function(numrows, numcols, initial) { 
const arr = [];
for (var i = 0; i < numrows; ++i) {
    let columns = [];
    for (let j = 0; j < numcols; ++j) {
                 columns[j] = initial;
              }
              arr[i] = columns;
           }
    return arr; 
}

console.log(Array.matrix(5,5,0));
/**
    [0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0]
*/

// ๋“ค์–ด๊ฐˆ ์š”์†Œ ๊ฐ’์„ ์•ˆ๋‹ค๋ฉด ์ง์ ‘ ๊ฐ’์„ ๋„ฃ์€ ๋ฐฐ์—ด๋กœ ์ƒ์„ฑ
let grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];
console.log(grades[2][2]); // 89

// Array.from() ํ™œ์šฉ
let arr = Array.from({length:5},()=>Array(5).fill(0));
/**
    [0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0]
    [0, 0, 0, 0, 0]
*/

 

์ด์ฐจ์› Array ๋‹ค๋ฃจ๊ธฐ

 

์ด์ฐจ์› array๋Š” ์š”์†Œ๋ฅผ (ํ–‰)row ๋˜๋Š” (๋ ฌ) column ์œ„์ฃผ๋กœ ์ ‘๊ทผํ•˜๋Š” ํŒจํ„ด์ด ์žˆ๋‹ค.

๋‘ ํŒจํ„ด์— ๋ชจ๋‘ ์ค‘์ฒฉ for๋ฌธ์„ ์‚ฌ์šฉํ•ด ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค.

 

s1,s2,s3๋Š” column ์ค‘์‹ฌ์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ, t1,t2,t3๋Š” row ์ค‘์‹ฌ์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋‹ค.

 

column์ค‘์‹ฌ์˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ์™ธ๋ถ€ ๋ฃจํ”„๋Š” row๋ฅผ ์ˆœํšŒํ•˜๊ณ , ๋‚ด๋ถ€ ๋ฃจํ”„๋Š” column์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.

๊ฐ ํ–‰(row)์ด ํ•œ ํ•™์ƒ์˜ ์„ฑ์ ์ด๊ณ  ๊ฐ cell์ด ์‹œํ—˜ ์ ์ˆ˜๋ผ๊ณ  ํ•  ๋•Œ, ๊ฐ๊ฐ์˜ ํ•™์ƒ๋“ค์˜ ํ‰๊ท  ์„ฑ์ ์„ ๊ตฌํ•˜๋Š” ๋‹ค์Œ ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณด์ž.

const grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];

// ๊ฐ ํ•™์ƒ์˜ ํ‰๊ท  grad์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด 
// ๊ฐ row์˜ ์š”์†Œ๋“ค์„ ๋”ํ•œ ๋’ค 
// row์˜ length๋งŒํผ ๋‚˜๋ˆ ์ฃผ๋ฉด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
let total = 0;
let average = 0.0;
for (let row = 0; row < grades.length; ++row) {
    for (let col = 0; col < grades[row].length; ++col) { 
        total += grades[row][col];
     }
     average = total / grades[row].length;
     console.log("Student " + parseInt(row+1) + " average: " +
         average.toFixed(2));
     total = 0;
     average = 0.0;
}
/**
'Student 1 average: 81.33'
'Student 2 average: 79.67'
'Student 3 average: 91.33'
*/

 

row์ค‘์‹ฌ์˜ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ์™ธ๋ถ€ ๋ฃจํ”„๋Š” column์„ ์ˆœํšŒํ•˜๊ณ , ๋‚ด๋ถ€ ๋ฃจํ”„๋Š” row๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.

์ด๋ฒˆ์—๋Š” ๊ฐ ์‹œํ—˜ ๋‹น ํ•™์ƒ๋“ค์˜ ํ‰๊ท ์„ ๊ตฌํ•˜๋Š” ๋ฒ•์„ ์‚ดํŽด๋ณด์ž.

const grades = [[89, 77, 78],[76, 82, 81],[91, 94, 89]];

// ๊ฐ ํ•™์ƒ์˜ ํ‰๊ท  grad์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด 
// ๊ฐ row์˜ ์š”์†Œ๋“ค์„ ๋”ํ•œ ๋’ค 
// row์˜ length๋งŒํผ ๋‚˜๋ˆ ์ฃผ๋ฉด ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.
let total = 0;
let average = 0.0;
for (let col = 0; col < grades.length; ++col) {
    for (let row = 0; row < grades[col].length; ++row) { 
        total += grades[row][col];
     }
     average = total / grades[col].length;
     console.log("Test " + parseInt(col+1) + " average: " +
         average.toFixed(2));
     total = 0;
     average = 0.0;
}
/**
'Test 1 average: 85.33'
'Test 2 average: 84.33'
'Test 3 average: 82.67'
*/

 

Jagged Arrays

Jagged Arrays๋ž€ array์— ๊ฐ row์˜ ๊ธธ์ด๊ฐ€ ๋‹ค๋ฅธ array๋ฅผ ๋œปํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์–ด๋–ค row๋Š” 3๊ฐœ์˜ ์š”์†Œ๋ฅผ, ๋‹ค๋ฅธ row๋Š” 5๊ฐœ์˜ ์š”์†Œ๋ฅผ, ๋˜ ๋‹ค๋ฅธ row๋Š” ์š”์†Œ๊ฐ€ 1๊ฐœ๋ฟ์ผ ์ˆ˜ ์žˆ๋Š” ์ค‘์ฒฉ array๋‹ค. ์ด๋Ÿฐ ๋“ค์‘ฅ๋‚ ์‘ฅํ•œ array๋ผ๊ณ  ํ•ด๋„ javascript for-loop์„ ํ†ตํ•ด ๋‚ด๋ถ€ array์˜ ๊ธธ์ด๋“ค์„ ๊ฐ๊ฐ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

let grades = [[89, 77],[76, 82, 81],[91, 94, 89, 99]]; 
let total = 0;
let average = 0.0;
for (var row = 0; row < grades.length; ++row) {
	for (var col = 0; col < grades[row].length; ++col) { 
    	total += grades[row][col];
   }
   average = total / grades[row].length;
   console.log("Student " + parseInt(row+1) + " average: " +
         average.toFixed(2));
   total = 0;
   average = 0.0;
}
/** 
Student 1 average: 83.00
    Student 2 average: 79.67
    Student 3 average: 93.25
*/

Object๋ฅผ ์š”์†Œ๋กœ ํ•˜๋Š”  Array

์ง€๊ธˆ๊นŒ์ง€ ์›์‹œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ๊ฐ’์„ ์š”์†Œ๋กœ ๊ฐ–๋Š” array์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ดค๋‹ค.

Array๋Š” ์›์‹œ๊ฐ’๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด, ํ•จ์ˆ˜, ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’ ๋“ฑ ๋ชจ๋‘๋ฅผ ์š”์†Œ๋กœ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.

function Point(x,y) { 
	this.x = x;
	this.y = y; 
}
function displayPts(arr) {
for (var i = 0; i < arr.length; ++i) {
   console.log(arr[i].x + ", " + arr[i].y);
   }
}
const p1 = new Point(1,2);
const p2 = new Point(3,5);
const p3 = new Point(2,8);
const p4 = new Point(4,4);
const points = [p1,p2,p3,p4];
for (let i = 0; i < points.length; ++i) {
       console.log("Point " + parseInt(i+1) + ": " + points[i].x + ", " +
             points[i].y);
}
const p5 = new Point(12,-3); 
points.push(p5); 
console.log("After push: ");
displayPts(points);
points.shift();
console.log("After shift: ");
displayPts(points);

/**
 Point 1: 1, 2
    Point 2: 3, 5
    Point 3: 2, 8
    Point 4: 4, 4
    After push:
    1, 2
    3, 5
    2, 8
    4, 4
    12, -3
    After shift:
    3, 5
2, 8 4, 4 12, -3

*/

 

Array๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” Object

์˜จ๋„ array๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๊ณ , ์˜จ๋„๋ฅผ ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•˜๊ณ , ํ‰๊ท ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด์˜ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž.

function weekTemps() { 
	this.dataStore = []; 
    this.add = add; 
    this.average = average;
}
function add(temp) { 
	this.dataStore.push(temp);
}
function average() {
	let total = 0;
	for (let i = 0; i < this.dataStore.length; ++i) {
		total += this.dataStore[i]; 
    }
	return total / this.dataStore.length; 
}
const thisWeek = new weekTemps(); 
thisWeek.add(52);
thisWeek.add(55);
thisWeek.add(61);
thisWeek.add(65);
thisWeek.add(55);
thisWeek.add(50);
thisWeek.add(52);
thisWeek.add(49);
console.log(thisWeek.average()); // 54.875

 

๋˜๋Š” ํด๋ž˜์Šค๋กœ ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๋˜‘๊ฐ™์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

push() ๋Œ€์‹  add ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ๋ฌด์—‡์„ ํ•˜๋Š”์ง€ ์ง๊ด€์ ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

class WeekTemps{
    constructor(){
        this.dataStore = [];
    }
    add(temp){
        this.dataStore.push(temp);
    }
    average(){
        let total = 0;
        for (let i = 0; i < this.dataStore.length; ++i) {
            total += this.dataStore[i]; 
        }
        return total / this.dataStore.length;
    }
}
const thisWeek = new WeekTemps();
thisWeek.add(52);
thisWeek.add(55);
thisWeek.add(61);
thisWeek.add(65);
thisWeek.add(55);
thisWeek.add(50);
thisWeek.add(52);
thisWeek.add(49);
console.log(thisWeek.average()); // 54.875

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋งˆ์น˜๋ฉด์„œ 

์˜ค๋Š˜์€ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด ๊นŠ์ด ์‚ดํŽด๋ณด๊ธฐ ์ „์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ๊ธฐ๋ณธ ์ž๋ฃŒ๊ตฌ์กฐ์ธ Array์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜๋‹ค.
์ฃผ๋กœ ๋‚ด์žฅ๋œ ํ•จ์ˆ˜๊ฐ€ ๋งŽ๊ณ , ๋ฐ์ดํ„ฐ ํƒ€์ž…์˜ ์ œํ•œ์ด ์—†์–ด์„œ array๋ฅผ ๋งŽ์ด ํ™œ์šฉํ–ˆ๋˜ ๊ฑฐ ๊ฐ™๋‹ค.
์ฑ…์ด ์ง€์–ด์ง„ ์ง€ (2014๋…„) 10๋…„์ด ๊ฑฐ์˜ ๋œ ์˜ค๋Š˜, array ๊ด€๋ จ ํ•จ์ˆ˜์™€ ์—ฌ๋Ÿฌ ๋ฌธ๋ฒ•๋“ค์ด ์ถ”๊ฐ€๋˜์—ˆ๋Š”๋ฐ ํ•ด๋‹น ์ฑ…์— ๋‹ค๋ฃจ์ง€ ๋ชปํ•ด์„œ ์•„์‰ฌ์› ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋‘ ํ•จ์ˆ˜๋ฅผ ์ด์–ด ์ฃผ๊ธฐ ์œ„ํ•ด์„œ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•( [...arr1, ...arr2])์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ์—๋Š” List์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž.

 

๐Ÿ“š Reference

- Data Structures and Algorithms Using Javaโ€ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9