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

task11ยท2022๋…„ 2์›” 14์ผ
1
post-thumbnail

๐Ÿ’ก ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์„ ์˜ˆ์ œ๋ฅผ ์ž‘์„ฑํ•˜๋ฉฐ ์•Œ์•„๋ณด์ž.

๊ฐœ์š” ๐Ÿ›ซ


์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์˜ ํŠน์ง•๊ณผ ๋ฐฐ์—ด์˜ ํ”„๋กœํ† ํƒ€์ž… ํ•จ์ˆ˜๋“ค์„ ์ •๋ฆฌํ•˜๊ณ , ๊ณต๋ถ€ํ•˜๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ ธ๋ณผ ๊ฒƒ์ด๋‹ค.

๋งค์šฐ ๋งค์šฐ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” reduce, filter .. ๋“ฑ๋“ฑ ๋ฐฐ์—ด ํ”„๋กœํ† ํƒ€์ž… ํ•จ์ˆ˜๋“ค์„ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ํŒŒํ—ค์ณ๋ณด๊ณ  ์ต์ˆ™ํ•ด์ง€๋Š” ์‹œ๊ฐ„์„ ๊ฐ€์ง„๋‹ค.

ํ•™์Šต ๋‚ด์šฉ ๐Ÿ“–


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์€ ๋‹ค๋ฅด๋‹ค ?

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์€ ๋‹ค๋ฅธ ์–ธ์–ด์—์„œ ๋งํ•˜๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด์ด ์•„๋‹Œ Hash ๊ธฐ๋ฐ˜์˜ ๊ฐ์ฒด์ด๋‹ค.

๋‹ค๋ฅธ ์–ธ์–ด์—์„œ์˜ ๋ฐฐ์—ด์ด๋ผ๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ์˜ ๊ฐœ๋…์€ ๋™์ผํ•œ ํฌ๊ธฐ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ๋นˆํ‹ˆ์—†์ด ์—ฐ์†์ ์œผ๋กœ ๋‚˜์—ด๋œ ์ž๋ฃŒ ๊ตฌ์กฐ๋ฅผ ๋งํ•˜๋ฉฐ
๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ํ•˜๋‚˜์˜ ํƒ€์ž…์œผ๋กœ ํ†ต์ผ๋˜์–ด ์žˆ๊ณ , ์„œ๋กœ ์—ฐ์†์ ์œผ๋กœ ์ธ์ ‘ํ•ด ์žˆ๋Š”๋ฐ€์ง‘ ๋ฐฐ์—ด(dense array)์ด๋‹ค.

๋ฐ€์ง‘ ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ๊ณ ์†์œผ๋กœ ํšจ์œจ์ ์ด๊ฒŒ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.(์‹œ๊ฐ„ ๋ณต์žก๋„ O(1))
ํ•˜์ง€๋งŒ ์ •๋ ฌ๋˜์ง€ ์•Š์€ ๋ฐฐ์—ด์—์„œ ํŠน์ •ํ•œ ๊ฐ’์„ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒฝ์šฐ, ๋ชจ๋“  ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๊ฐ’์„ ๋ฐœ๊ฒฌํ•  ๋•Œ๊นŒ์ง€ ์ฐจ๋ก€๋Œ€๋กœ ํƒ์ƒ‰ํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. (์‹œ๊ฐ„ ๋ณต์žก๋„ O(n))

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์œ„ํ•œ ๊ฐ๊ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์€ ๋™์ผํ•œ ํฌ๊ธฐ๋ฅผ ๊ฐ–์ง€ ์•Š์•„๋„ ๋˜๋ฉฐ ์—ฐ์†์ ์œผ๋กœ ์ด์–ด์ ธ ์žˆ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค.
๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ์ด์–ด์ ธ ์žˆ์ง€ ์•Š๋Š” ๋ฐฐ์—ด์„ ํฌ์†Œ ๋ฐฐ์—ด(sparse array)์ด๋ผ ํ•œ๋‹ค.

์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค๋กœ ๋ฐฐ์—ด ์š”์†Œ์— ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ํŠน์ • ์š”์†Œ๋ฅผ ํƒ์ƒ‰ํ•˜๊ฑฐ๋‚˜ ์š”์†Œ๋ฅผ ์‚ฝ์ž… ๋˜๋Š” ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ํšจ์œจ์ ์ด์ง€ ์•Š๋‹ค.
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด์€ ํ•ด์‹œ ํ…Œ์ด๋ธ”๋กœ ๊ตฌํ˜„๋œ ๊ฐ์ฒด์ด๋ฏ€๋กœ ์ธ๋ฑ์Šค๋กœ ๋ฐฐ์—ด ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ, ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด๋ณด๋‹ค ์„ฑ๋Šฅ์ ์ธ ๋ฉด์—์„œ ๋Š๋ฆด ์ˆ˜ ๋ฐ–์— ์—†๋Š” ๊ตฌ์กฐ์ ์ธ ๋‹จ์ ์„ ๊ฐ–๋Š”๋‹ค. ํ•˜์ง€๋งŒ ํŠน์ • ์š”์†Œ๋ฅผ ํƒ์ƒ‰ํ•˜๊ฑฐ๋‚˜ ์š”์†Œ๋ฅผ ์‚ฝ์ž… ๋˜๋Š” ์‚ญ์ œํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด๋ณด๋‹ค ๋น ๋ฅธ ์„ฑ๋Šฅ์„ ๊ธฐ๋Œ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด์ฒ˜๋Ÿผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์€ ์—„๋ฐ€ํžˆ ๋งํ•ด ์ผ๋ฐ˜์  ์˜๋ฏธ์˜ ๋ฐฐ์—ด์ด ์•„๋‹ˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์€ ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด์˜ ๋™์ž‘์„ ํ‰๋‚ด๋‚ธ ํŠน์ˆ˜ํ•œ ๊ฐ์ฒด์ด๋‹ค.

let nums = [];

nums.push("one");
nums.push("two");
console.log(nums.length); // 2
console.log(nums); // [ 'one', 'two' ]

console.log(Object.getOwnPropertyDescriptors(nums));
// {
//   '0': {
//     value: 'one',
//     writable: true,
//     enumerable: true,
//     configurable: true
//   },
//   '1': {
//     value: 'two',
//     writable: true,
//     enumerable: true,
//     configurable: true
//   },
//   length: { value: 2, writable: true, enumerable: false, configurable: false }
// }

๐Ÿ’ก ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค๋ฅผ ํ”„๋กœํผํ‹ฐ ํ‚ค๋กœ ๊ฐ–์œผ๋ฉฐ length ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š” ํŠน์ˆ˜ํ•œ ๊ฐ์ฒด์ด๋‹ค.

์œ ์‚ฌ ๋ฐฐ์—ด

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์€ ๊ฐ์ฒด์ด๋ฏ€๋กœ ๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๋ฉด ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

์•„๋ž˜๋Š” ์œ ์‚ฌ๋ฐฐ์—ด์˜ ์˜ˆ์‹œ์ด๋‹ค.

let arr = [1, 2, 3];
let nodes = document.querySelectorAll('div'); // NodeList [div, div ...]

// ์œ ์‚ฌ ๋ฐฐ์—ด
let arrLikeObj = {
  0: 'a',
  1: 'b',
  2: 'c',
  3: 'd',
  length: 4
};

Array.isArray(array); // true
Array.isArray(nodes); // false
Array.isArray(arrLikeObj); // false

์œ„ ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด arrLikeObj ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด๊ณผ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

let arrLikeObj = {
  0: 'a',
  1: 'b',
  2: 'c',
  3: 'd',
  length: 4
};

console.log(arrLikeObj[0]);     // a
console.log(arrLikeObj[1]);     // b
console.log(arrLikeObj[2]);     // c
console.log(arrLikeObj[3]);     // d
console.log(arrLikeObj.length); // 4

์ด๋ ‡๊ฒŒ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌ ๋ฐฐ์—ด์„ ๊ตฌ๋ถ„ํ•ด์•ผํ•˜๋Š” ์ด์œ ๋Š” ์œ ์‚ฌ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ ๋ฐฐ์—ด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์“ธ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

์œ ์‚ฌ๋ฐฐ์—ด์€ call์ด๋‚˜ apply๋ฅผ ์‚ฌ์šฉํ•ด ๋ฐฐ์—ด ํ”„๋กœํ† ํƒ€์ž…์—์„œ ๋ฉ”์„œ๋“œ๋ฅผ ๋นŒ๋ ค ์จ์•ผํ•œ๋‹ค.

// forEach ๋นŒ๋ฆฌ๊ธฐ
Array.prototype.forEach.call(arrLikeObj, function(item) { console.log(item); });
[].forEach.call(arrLikeObj, function(item) { console.log(item); });

// ES6
Array.from(arrLikeObj).forEach(function(item) { console.log(item) });

๋ฉ”์„œ๋“œ๋ฅผ ๋นŒ๋ ค ์œ ์‚ฌ ๋ฐฐ์—ด์„ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ๋™์ž‘์‹œํ‚ค๋Š” ๊ฒƒ์€ ์›น ํ”„๋กœ์ ํŠธ๋ฅผ ํ•  ๋•Œ API ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•˜๋Š” ๊ณผ์ •์—์„œ ์‚ฌ์šฉํ–ˆ๋˜ ๊ธฐ์–ต์ด ์žˆ๋‹ค.


์˜ˆ์ œ๋กœ ๋ณด๋Š” ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ

์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์ •๋ฆฌ

1. push(pop) / shift(unshift) - ๋ฐฐ์—ด ์ถ”๊ฐ€/์‚ญ์ œ

  1. LIFO - Back : ๋’ค์—์„œ๋ถ€ํ„ฐ ์ž‘๋™
    ์ถ”๊ฐ€ : Array.push(item)
    ์‚ญ์ œ : Array.pop()
let 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 ]

nums.pop();
console.log(nums); // [ 1, 2, 3, 4, 5 ]
  1. LIFO - Front : ์•ž์—์„œ๋ถ€ํ„ฐ ์ž‘๋™
    ์ถ”๊ฐ€ : Array.unshift(item)
    ์‚ญ์ œ : Array.shift()
let nums = [1, 2, 3, 4, 5];

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

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

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

2. splice - ๋ฐฐ์—ด ์‚ญ์ œ/๋ณ€๊ฒฝ (index)

  1. ์‚ญ์ œ/๋ณ€๊ฒฝ : Array.splice(index[, deleteCount, elem1,...elemN])
let uname = ['๊น€์ •ํ˜„', '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ', '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด'];
let ref;
ref = uname.splice(3);
console.log(ref);   // [ '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด' ]
console.log(uname); // [ '๊น€์ •ํ˜„', '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ' ]

ref = uname.splice(1, 1);
console.log(ref);   // [ '๋ฐ•์ •๋ฏผ' ]
console.log(uname); // [ '๊น€์ •ํ˜„', '์ดํ˜ธ์„ญ' ]

ref = uname.splice(1, 1, 'noob');
console.log(ref);   // [ '์ดํ˜ธ์„ญ' ]
console.log(uname); // [ '๊น€์ •ํ˜„', 'noob' ]

splice๋Š” ์›๋ณธ์ด ํ›ผ์†๋œ๋‹ค.

3. slice - ๋ฐฐ์—ด ์‚ญ์ œ (index)

  1. ์‚ญ์ œ : Array.slice([start], [end])
let uname = ['๊น€์ •ํ˜„', '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ', '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด'];

// 1(์ œ์™ธ) ์˜ค๋ฅธ์ชฝ ์ถœ๋ ฅ
console.log(uname.slice(1)); // [ '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ', '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด' ]

// 3(์ œ์™ธ) ์˜ค๋ฅธ์ชฝ ์ถœ๋ ฅ
console.log(uname.slice(3)); // [ '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด' ]

// 1(์ œ์™ธ) ~ 3 ์‚ฌ์ด ์ถœ๋ ฅ
console.log(uname.slice(1, 3)); // [ '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ' ]

// -2(ํฌํ•จ) ์˜ค๋ฅธ์ชฝ ์ถœ๋ ฅ
console.log(uname.slice(-2)); // [ '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด' ]

// ์›๋ณธ ์œ ์ง€
console.log(uname); // [ '๊น€์ •ํ˜„', '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ', '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด' ]

slice๋Š” ์›๋ณธ์ด ์œ ์ง€๋œ๋‹ค.

4. concat - ๋ฐฐ์—ด ๋ณ‘ํ•ฉ

  1. ๋ณ‘ํ•ฉ : Array.concat(arg1, arg2, arg3, ...)
let uname = ['๊น€์ •ํ˜„', '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ', '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด']; 

console.log(uname.concat('noob1')); // [ '๊น€์ •ํ˜„', '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ', '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด', 'noob1' ]

console.log(uname.concat(['noob1', 'noob2'], 'noob3')); // [ '๊น€์ •ํ˜„', '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ', '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด', 'noob1', 'noob2', 'noob3' ]

// ์›๋ณธ ํ›ผ์† X
console.log(uname); // [ '๊น€์ •ํ˜„', '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ', '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด' ]

5. for-loop(index) / for...of(item) / for...in(key) - ๋ฐฐ์—ด ํƒ์ƒ‰

  1. Common for-loop(index)
let uname = ['๊น€์ •ํ˜„', '๋ฐ•์ •๋ฏผ', '์ดํ˜ธ์„ญ', '์ตœ๊ตญ์ง„', '์œ ๋ฐ”๋ณด'];

for (let i = 0; i < uname.length; i++) {
  console.log(uname[i]);
}
// output : ๊น€์ •ํ˜„ ๋ฐ•์ •๋ฏผ ์ดํ˜ธ์„ญ ์ตœ๊ตญ์ง„ ์œ ๋ฐ”๋ณด
  1. for...of(item)
for (let name of uname) {
  console.log(name);
}
// output : ๊น€์ •ํ˜„ ๋ฐ•์ •๋ฏผ ์ดํ˜ธ์„ญ ์ตœ๊ตญ์ง„ ์œ ๋ฐ”๋ณด
  1. for...in(key)
for (let key in uname) {
  console.log(uname[key]);
}
// output : ๊น€์ •ํ˜„ ๋ฐ•์ •๋ฏผ ์ดํ˜ธ์„ญ ์ตœ๊ตญ์ง„ ์œ ๋ฐ”๋ณด

6. indexOf / lastIndexOf / includes - ๋ฐฐ์—ด ํƒ์ƒ‰

  1. item์˜ index ํƒ์ƒ‰(front) : Array.indexOf(item)
let uname = ['e', 'c', 'f', 'a', 'd', 'b', 'a'];

console.log(uname.indexOf('a')); // index : 3
console.log(uname.indexOf('z')); // index : -1 (์กด์žฌ X)
console.log(uname.indexOf('a', 4)); // index : 6 (index 4 ์ดํ›„์˜ ์ค‘๋ณต item index)
console.log(uname.indexOf('a', uname.indexOf('a') + 1)); // index : 6 (์ฒซ ๋ฒˆ ์จฐ item์˜ ์ค‘๋ณต item index)
  1. item์˜ index ํƒ์ƒ‰(back) : Array.lastIndexOf(item)
console.log(uname.lastIndexOf('a')); // index : 6 
console.log(uname.lastIndexOf('a', -2)); // index : 3
  1. item์˜ include ์—ฌ๋ถ€ : Array.includes(item)
console.log(uname.includes('a')); // true
console.log(uname.includes('z')); // false

7. sort / reverse - ๋ฐฐ์—ด ์ •๋ ฌ

  1. ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ : Array.sort()
let abc = ['e', 'c', 'f', 'a', 'd', 'b', 'a'];
let nums = [5, 3, 6, 1, 2, 4, 7];

console.log(abc.sort()); // ['a', 'a', 'b', 'c', 'd', 'e','f']

console.log(nums.sort()); // [1, 2, 3, 4, 5, 6, 7]

sort ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ฌธ์ œ์ ๊ณผ ์ •๋ ฌ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ, sort์— ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด ์กฐ๊ฑด ๋ถ€ ์ •๋ ฌ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์•„๋ž˜ ๊ณ ์ฐจ ํ•จ์ˆ˜ ๋ถ€๋ถ„ ์—์„œ ์˜ˆ์ œ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

  1. ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ : Array.reverse()
console.log(abc.reverse()); // [ 'f', 'e', 'd', 'c', 'b', 'a', 'a']

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

8. join - ๋ฐฐ์—ด ๋ณ€ํ™˜

  1. ๋ฐฐ์—ด ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ : Array.join(separator)
let hello = ['์•ˆ', '๋…•', 'ํ•˜', '์„ธ', '์š”', ' ', 'join', '์˜ˆ์ œ', '์ž…๋‹ˆ๋‹ค.'];

console.log(hello.join('~')); // ์•ˆ~๋…•~ํ•˜~์„ธ~์š”~ ~join~์˜ˆ์ œ~์ž…๋‹ˆ๋‹ค.
console.log(hello.join('')); // ์•ˆ๋…•ํ•˜์„ธ์š” join์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

let str = hello.join('!');
console.log(str); // ์•ˆ!๋…•!ํ•˜!์„ธ!์š”! !join!์˜ˆ์ œ!์ž…๋‹ˆ๋‹ค.

console.log(str.split('!')); // ['์•ˆ', '๋…•', 'ํ•˜', '์„ธ', '์š”', ' ', 'join', '์˜ˆ์ œ', '์ž…๋‹ˆ๋‹ค.']

+ ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ (๊ณ ์ฐจ ํ•จ์ˆ˜)

1. sort(callback function)์ž„์˜์ •๋ ฌ

sort ์ •๋ ฌ issue

  1. ์ˆซ์ž์˜ ์ž๋ฆฌ์ˆ˜ ์ •๋ ฌ ๋ฌธ์ œ : sort ๋ฉ”์„œ๋“œ๋กœ ์š”์†Œ๊ฐ€ ์ •๋ ฌ๋  ๋•Œ ์ผ์‹œ์ ์œผ๋กœ ์š”์†Œ๋“ค์„ ๋ฌธ์ž์—ด๋กœ ๋ณ€๊ฒฝ์‹œํ‚ค๋Š”๋ฐ, ์ด ๋•Œ ASCII ๋ฌธ์ž ์ˆœ์„œ๋กœ ์ •๋ ฌ๋˜์–ด ์ˆซ์ž์˜ ํฌ๊ธฐ๋Œ€๋กœ ๋‚˜์˜ค์ง€ ์•Š๋Š” ๋ฌธ์ œ
let nums = [10, 2, 4, 3, 5, 20, 7, -1, 9, 1];

console.log(nums.sort()); // [ -1, 1, 10, 2, 20, 3, 4, 5, 7, 9]
console.log(nums.reverse()); // [9, 7, 5, 4, 3, 20, 2, 10, 1, -1]
  1. ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„๋˜์–ด ์ •๋ ฌ : ์›์ธ์€ ์œ„์™€ ๊ฐ™์Œ
let abc = ['e', 'c', 'f', 'a', 'd', 'F', 'b', 'A'];

console.log(abc.sort()); // ['A', 'F', 'a', 'b', 'c', 'd', 'e', 'f']
console.log(abc.reverse()); // ['f', 'e', 'd', 'c', 'b', 'a', 'F', 'A']

sort(callbackFn)์œผ๋กœ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ

sort ๋ฉ”์„œ๋“œ๋Š” (method) Array<number>.sort(compareFn?: (a: number, b: number) => number) ๋กœ ๋‘ ์ˆ˜์˜ ๊ฐ’์„ ๋น„๊ตํ•ด์„œ ๋™์ž‘ํ•˜๋Š” ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

  1. number ํƒ€์ž… ์ •๋ ฌ
let nums = [10, 2, 4, 3, 5, 20, 7, -1, 9, 1];

// ์˜ค๋ฆ„์ฐจ์ˆœ
nums.sort(function (x, y) {
  return x - y;
})

console.log(nums);

// ๋‚ด๋ฆผ์ฐจ์ˆœ
nums.sort(function (x, y) {
  return y - x;
})

console.log(nums);
  1. string ํƒ€์ž… ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์ด ์ •๋ ฌ
let abc = ['e', 'c', 'f', 'a', 'd', 'F', 'b', 'A'];

// ์˜ค๋ฆ„์ฐจ์ˆœ
abc.sort(function (x, y) {
  x = x.toUpperCase();
  y = y.toUpperCase();

  if (x > y) return 1;
  else if (x < y) return -1;
  else return 0;
})

console.log(abc);

// ๋‚ด๋ฆผ์ฐจ์ˆœ
abc.sort(function (x, y) {
  x = x.toUpperCase();
  y = y.toUpperCase();

  if (x < y) return 1;
  else if (x > y) return -1;
  else return 0;
})

console.log(abc);
  1. number, string ํƒ€์ž… ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„์—†๋Š” ์ •๋ ฌ (์œ„ ์ •๋ ฌ์ฝ”๋“œ๋ฅผ ํ•˜๋‚˜๋กœ)
const ascendingOrder = function (x, y) {
  if (typeof x === "string") x = x.toUpperCase();
  if (typeof y === "string") y = y.toUpperCase();

  return x > y ? 1 : -1;
}

const descendingOrder = function(x, y) {
  if (typeof x === "string") x = x.toUpperCase();
  if (typeof y === "string") y = y.toUpperCase();

  return x < y ? 1 : -1;
}

nums.sort(ascendingOrder);
abc.sort(descendingOrder);

2. forEach() - ์š”์†Œ๋ณ„ ์‹คํ–‰

์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์‹คํ–‰ : Array.forEach(function(item, index, array){})

  1. ๊ธฐ๋ณธ ์˜ˆ์ œ
let abc = ['a', 'b', 'c', 'd'];

abc.forEach(function (i) {
  console.log(i);
})

abc.forEach(function (item, index) {
  console.log(item + ' foreach');
})

// a 
// b 
// c 
// d 

// a foreach
// b foreach
// c foreach
// d foreach

3. map() - ์š”์†Œ๋ณ„ ์‹คํ–‰(๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜)

์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ : Array.map(function(item, index, array){})

  1. ๊ธฐ๋ณธ ์˜ˆ์ œ
let abc = ['a', 'b', 'c', 'd'];

let abcMap = abc.map(function (item) {
  return item + ' map ์ˆœํšŒ';
})

for (let item of abcMap) {
  console.log(item);
}

// a map ์ˆœํšŒ
// b map ์ˆœํšŒ
// c map ์ˆœํšŒ
// d map ์ˆœํšŒ

4. find() - ์กฐ๊ฑด๋งŒ์กฑํ•˜๋Š” ๊ฐ’ ํ•˜๋‚˜ ๋ฐ˜ํ™˜

์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ : Array.find(function(item, index, array){})

  1. ๊ธฐ๋ณธ ์˜ˆ์ œ
let person = [
  {
    name: '๊น€์ •ํ˜„',
    age: 28
  },
  {
    name: '๋ฐฐ์œค์ฃผ',
    age: 26
  },
  {
    name: '๊น€๊ธธ๋™',
    age: 16
  },
  {
    name: '๋ฐ•๊ธธ๋™',
    age: 18
  }
];

let findName = person.find(function (user) {
  return user.name === '๊น€์ •ํ˜„';
});

let findAge = person.find(function (user) {
  return user.age < 18;
})

console.log(findName); // { name: '๊น€์ •ํ˜„', age: 28 }
console.log(findAge); // { name: '๊น€๊ธธ๋™', age: 16 }

5. filter() - ์กฐ๊ฑด๋งŒ์กฑํ•˜๋Š” ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜

์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ : Array.filter(function(item, index, array){})

  1. ๊ธฐ๋ณธ ์˜ˆ์ œ
let person = [
  {
    name: '๊น€์ •ํ˜„',
    age: 28
  },
  {
    name: '๋ฐฐ์œค์ฃผ',
    age: 26
  },
  {
    name: '๊น€๊ธธ๋™',
    age: 16
  },
  {
    name: '๋ฐ•๊ธธ๋™',
    age: 18
  }
];

let findAge = person.filter(function (user) {
  return user.age > 18;
})

console.log(findAge); // [ { name: '๊น€์ •ํ˜„', age: 28 }, { name: '๋ฐฐ์œค์ฃผ', age: 26 } ]

6. reduce() - ์š”์†Œ ๋ณ„ ์‹คํ–‰(๋ˆ„์  ๊ฐ’ ๋ฐ˜ํ™˜)

์š”์†Œ ๋ณ„ ํ•จ์ˆ˜ ์ˆ˜ํ–‰ ๋ˆ„์  ๊ฒฐ๊ณผ๊ฐ’ ๋ฐ˜ํ™˜ : Array.reduce(function(accumulator, item, index, array){})

  1. ๊ธฐ๋ณธ ์˜ˆ์ œ
let nums = [1, 2, 3, 4, 5, 6, 7];
let count = 0;

let sum = nums.reduce(function (acc, item, index, array) {
  count++
  console.log(acc, " ", item, " ", index);
  return acc + item;
}, 0); // acc๊ฐ’ initํ•˜๋Š” ๋ถ€๋ถ„(default : 1)

console.log("count : " + count);

// 0   1   0
// 1   2   1
// 3   3   2
// 6   4   3
// 10   5   4
// 15   6   5
// 21   7   6
// count : 7

7. some() - ์š”์†Œ์˜ ์กฐ๊ฑด ๋งŒ์กฑ์‹œ ์ฐธ

๋ฐฐ์—ด ๋‚ด ๋‹จ ํ•˜๋‚˜๋ผ๋„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด true
Array.some(function(item, index, array){})

let person = [
  {
    name: '๊น€์ •ํ˜„',
    age: 28
  },
  {
    name: '๋ฐฐ์œค์ฃผ',
    age: 26
  },
  {
    name: '๊น€๊ธธ๋™',
    age: 16
  },
  {
    name: '๋ฐ•๊ธธ๋™',
    age: 18
  }
];

let someAge = person.some(function (user) {
  return user.age == 28;
})

console.log(someAge); // true

8. every() - ์š”์†Œ ๋ชจ๋‘ ์กฐ๊ฑด ๋งŒ์กฑ์‹œ ์ฐธ

๋ฐฐ์—ด ๋‚ด ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด true
Array.every(function(item, index, array){})

let person = [
  {
    name: '๊น€์ •ํ˜„',
    age: 28
  },
  {
    name: '๋ฐฐ์œค์ฃผ',
    age: 26
  },
  {
    name: '๊น€๊ธธ๋™',
    age: 16
  },
  {
    name: '๋ฐ•๊ธธ๋™',
    age: 18
  }
];

let everyAge = person.every(function (user) {
  return user.age > 15;
})

console.log(everyAge); // true

Review ๐Ÿ’ก

์ด๋ฒˆ ํฌ์ŠคํŠธ์—๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ , ์ผ๋ฐ˜ ๋ฐฐ์—ด๊ณผ์˜ ์ฐจ์ด์ ์— ๋Œ€ํ•ด ๊ณต๋ถ€ํ•ด๋ณด์•˜๋‹ค.

๋˜ํ•œ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐฐ์—ด์˜ ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ 14๊ฐ€์ง€ ์ •๋„๋ฅผ ์‚ดํŽด๋ณด์•˜๊ณ . ๊ฐ๊ฐ์˜ ๋™์ž‘์›๋ฆฌ์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ณ , ์˜ˆ์ œ ์ฝ”๋“œ๋กœ ์‹ค์Šต์„ ํ•ด๋ณด์•˜๋‹ค.

์ด์ „ ๊ฐœ์ธ ํ”„๋กœ์ ํŠธ ๋“ฑ์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ reduce, filter, map ๋“ฑ ํ•จ์ˆ˜๋“ค์„ ์ž์ฃผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, ์ด๋ฒˆ ๊ธฐํšŒ์— ์ •๋ฆฌ๋ฅผ ํ•ด๋ณด๋‹ˆ ๋” ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

API ํ˜ธ์ถœ๋กœ ๊ฐ€์ ธ์˜ค๋Š” JSON Data์˜ ๊ฐ€๊ณต์ด๋‚˜ Data ํ•„ํ„ฐ ์กฐํšŒ ์‹œ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ,, ๊ฐ์ฒด์˜ depth๊ฐ€ ๊นŠ์–ด์งˆ ์ˆ˜๋ก ํ•จ์ˆ˜๋ฅผ ์งœ๊ธฐ๊ฐ€ ์–ด๋ ค์› ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ๊ธฐ์ดˆ๋ฅผ ๋‹จ๋‹จํžˆ ํ•˜์˜€์œผ๋‹ˆ ๋‹ค์Œ์—” ๋” ํšจ์œจ์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค.

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