๋ฐฐ์—ด(Array)

JOY๐ŸŒฑยท2023๋…„ 2์›” 8์ผ
0

๐ŸŒย JavaScript

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

๐Ÿ’โ€โ™€๏ธ ๋ฐฐ์—ด(Array)์ด๋ž€,
์ด๋ฆ„๊ณผ ์ธ๋ฑ์Šค๋กœ ์ฐธ์กฐ๋˜๋Š” ์ •๋ ฌ๋œ ๊ฐ’์˜ ์ง‘ํ•ฉ. ๋ฐฐ์—ด์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐ๊ฐ์˜ ๊ฐ’์„ ๋ฐฐ์—ด ์š”์†Œ(element)๋ผ๊ณ  ํ•˜๋ฉฐ, ๋ฐฐ์—ด์—์„œ์˜ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ˆซ์ž๋ฅผ ์ธ๋ฑ์Šค(index)๋ผ๊ณ  ํ•จ.


๐Ÿ‘€ ๋ฐฐ์—ด

1) ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ํ†ตํ•œ ์ƒ์„ฑ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ "๋ชจ๋“  ๊ฐ’"์€ ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ

const arr = ['๐ŸŒ', '๐Ÿ‘', '๐Ÿฅ'];
console.log(arr);

2) ๋ฐฐ์—ด ์ƒ์„ฑ์ž ํ•จ์ˆ˜

const arr2 = new Array();
console.log(arr2); // []

[1] ์ „๋‹ฌ๋œ ์ธ์ž๊ฐ€ ์ˆซ์ž์ด๊ณ , ํ•˜๋‚˜๋งŒ ์ „๋‹ฌ๋˜์—ˆ๋‹ค๋ฉด ๋ฐฐ์—ด์˜ ๊ณต๊ฐ„ ์ƒ์„ฑ์œผ๋กœ ์ธ์‹

const arr3 = new Array(10);
console.log(arr3); // [ <10 empty items> ]

[2] ์ „๋‹ฌ๋œ ์ธ์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ด๊ฑฐ๋‚˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ ์ธ์ˆ˜๋ฅผ ์š”์†Œ๋กœ ๊ฐ–๋Š” ๋ฐฐ์—ด ์ƒ์„ฑ

const arr4 = new Array('๐Ÿ ', '๐Ÿง€', '๐Ÿฅ•', '๐Ÿž');
console.log(arr4);

3) Array.of ๋ฉ”์†Œ๋“œ

[1] ์ „๋‹ฌ ๋œ ์ธ์ˆ˜๋ฅผ ์š”์†Œ๋กœ ๊ฐ–๋Š” ๋ฐฐ์—ด ์ƒ์„ฑ

console.log(Array.of(10));              // [ 10 ]
console<.log(Array.of(1, 2, 3));         // [ 1, 2, 3 ]
console.log(Array.of("Hi", "there!"));  // [ 'Hi', 'there!' ]

4) ๋ฐฐ์—ด์˜ ํŠน์ง•

[1] ๋ฐฐ์—ด์˜ ์š”์†Œ๋Š” ์ž์‹ ์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ง€๋ฉฐ ๋ฐฐ์—ด์˜ ์š”์†Œ์— ์ ‘๊ทผํ•  ๋•Œ ์‚ฌ์šฉ

console.log(arr[0]);    // ๐ŸŒ
console.log(arr[1]);    // ๐Ÿ‘
console.log(arr[2]);    // ๐Ÿฅ

[2] ๋ฐฐ์—ด์€ ์š”์†Œ์˜ ๊ฐœ์ˆ˜, ์ฆ‰ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” length ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง

console.log(arr.length);

[3] for๋ฌธ์„ ํ†ตํ•œ ์š”์†Œ ์ˆœ์ฐจ ์ ‘๊ทผ

for(let i = 0; i < arr.length; i++) {
    console.log(arr[i]);
}

[4] ๋ฐฐ์—ด์€ ๋ณ„๋„์˜ ํƒ€์ž…์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉฐ ๊ฐ์ฒด ํƒ€์ž…

console.log(typeof arr); // object

๐Ÿ‘€ ์ผ๋ฐ˜ ๋ฐฐ์—ด๊ณผ์˜ ์ฐจ์ด

  • ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด : ๊ฐ ์š”์†Œ๊ฐ€ ๋™์ผํ•œ ๋ฐ์ดํ„ฐ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง€๋ฉฐ, ๋นˆํ‹ˆ ์—†์ด ์—ฐ์†์ ์œผ๋กœ ์ด์–ด์ ธ ์žˆ์–ด ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ์ž„์˜์˜ ์š”์†Œ์— ํ•œ ๋ฒˆ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ์† ๋™์ž‘์ด ์žฅ์ 
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด : ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด์˜ ๋™์ž‘์„ ํ‰๋‚ด๋‚ธ ํŠน์ˆ˜ํ•œ ๊ฐ์ฒด. ๊ฐ๊ฐ์˜ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์ด ๋™์ผํ•œ ํฌ๊ธฐ๋ฅผ ๊ฐ–์ง€ ์•Š์•„๋„ ๋˜๊ณ  ์—ฐ์†์ ์œผ๋กœ ์ด์–ด์ ธ ์žˆ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Œ. ์ธ๋ฑ์Šค๋กœ ๋ฐฐ์—ด ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ ์ผ๋ฐ˜์ ์ธ ๋ฐฐ์—ด๋ณด๋‹ค ๋А๋ฆฌ์ง€๋งŒ ์š”์†Œ์˜ ์‚ฝ์ž…, ์‚ญ์ œ์˜ ๊ฒฝ์šฐ ๋น ๋ฆ„.
// ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฐ’์ด ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์ด ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ
// ๋ชจ๋“  ๊ฐ’์ด ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Œ
const arr = [
    '์น˜์ฆˆ',
    '2',
    true,
    null,
    undefined,
    NaN,
    Infinity,
    [],             // ๋ฐฐ์—ด
    {},             // ๊ฐ์ฒด
    function() {}   // ํ•จ์ˆ˜
];

๐Ÿ‘€ length property

[1] length property๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” 0์ด์ƒ์˜ ์ •์ˆ˜๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ€์ง

console.log([].length);         // 0
const arr = [1, 2, 3, 4, 5];    
console.log(arr.length);        // 5

[2] length property ๊ฐ’์€ ๋ฐฐ์—ด์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๋ฉด ์ž๋™์œผ๋กœ ๊ฐฑ์‹ 

arr.push(6);
console.log(arr);           // [ 1, 2, 3, 4, 5, 6 ]
console.log(arr.length);    // 6
arr.pop();
console.log(arr);           // [ 1, 2, 3, 4, 5 ]
console.log(arr.length);    // 5

[3] length property์— ์ž„์˜์˜ ์ˆซ์ž ๊ฐ’์„ ๋ช…์‹œ์ ์œผ๋กœ ํ• ๋‹น ๊ฐ€๋Šฅ

arr.length = 3;
console.log(arr);           // [ 1, 2, 3 ]
console.log(arr.length);    // 3

[4] ํ˜„์žฌ length๋ณด๋‹ค ํฐ ์ˆซ์ž๋ฅผ ํ• ๋‹นํ•˜๋ฉด length ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์€ ๋ณ€๊ฒฝ๋˜์ง€๋งŒ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚˜์ง€๋Š” ์•Š์Œ

arr.length = 10;
console.log(arr);           // [ 1, 2, 3, <7 empty items> ]
console.log(arr.length);    // 10
console.log(Object.getOwnPropertyDescriptors(arr));

[5] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ์œ„์น˜ํ•˜์ง€ ์•Š๊ณ  ์ผ๋ถ€๊ฐ€ ๋น„์–ด์žˆ๋Š” ํฌ์†Œ ๋ฐฐ์—ด์„ ๋ฌธ๋ฒ•์ ์œผ๋กœ ํ—ˆ์šฉ

const sparse = [, 2, , 4];
console.log(sparse);        // [ <1 empty item>, 2, <1 empty item>, 4 ]
console.log(sparse.length); // 4
console.log(Object.getOwnPropertyDescriptors(sparse));

๐Ÿ‘€ ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ

๐Ÿ’โ€โ™€๏ธ Array.prototype์€ ๋ฐฐ์—ด์„ ์œ„ํ•œ ๋นŒํŠธ์ธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณต

const arr = [];
// ๋ฐฐ์—ด์˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” Array
console.log(arr.constructor === Array);                         // true
// ๋ฐฐ์—ด์˜ ํ”„๋กœํ† ํƒ€์ž… ๊ฐ์ฒด๋Š” Array.prototype
console.log(Object.getPrototypeOf(arr) === Array.prototype);    // true
// Array.prototype์ด ๋ฐฐ์—ด์„ ์œ„ํ•œ ๋นŒํŠธ์ธ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ ํ™•์ธ

1) indexOf

indexOf() : ๋ฐฐ์—ด์—์„œ ์š”์†Œ๊ฐ€ ์œ„์น˜ํ•œ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ด
lastIndexOf() : ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ์œ„์น˜ํ•œ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ๋ฆฌํ„ด
includes() : ๋ฐฐ์—ด์— ํ•ด๋‹น ์š”์†Œ ํฌํ•จ ์—ฌ๋ถ€ ๋ฆฌํ„ด

const foodList = ['๋ฌผํšŒ', '์‚ผ๊ณ„ํƒ•', '๋ƒ‰๋ฉด', '์ˆ˜๋ฐ•', '๋ฌผํšŒ'];

console.log(`foodList.indexOf('๋ฌผํšŒ') : ${foodList.indexOf('๋ฌผํšŒ')}`);                  // 0
console.log(`foodList.indexOf('๋ฌผํšŒ', 1) : ${foodList.indexOf('๋ฌผํšŒ', 1)}`);            // 4    // 1 ์ดํ›„๋ถ€ํ„ฐ ์ฐพ๊ธฐ
console.log(`foodList.indexOf('์‚ผ๊ฒน์‚ด') :  ${foodList.indexOf('์‚ผ๊ฒน์‚ด')}`);             // -1   // -1 : ์ธ๋ฑ์Šค๊ฐ€ ์—†๋‹ค๋Š” ์˜๋ฏธ

console.log(`foodList.lastIndexOf('๋ฌผํšŒ') : ${foodList.lastIndexOf('๋ฌผํšŒ')}`);          // 4
console.log(`foodList.lastIndexOf('๋ฌผํšŒ', 1) : ${foodList.lastIndexOf('๋ฌผํšŒ', 1)}`);    // 0    // 1 ์ด์ „๋ถ€ํ„ฐ ๊ฑฐ๊พธ๋กœ ์ฐพ๊ธฐ
console.log(`foodList.lastIndexOf('์‚ผ๊ฒน์‚ด') : ${foodList.lastIndexOf('์‚ผ๊ฒน์‚ด')}`);      // -1

console.log(`foodList.includes('๋ฌผํšŒ') : ${foodList.includes('๋ฌผํšŒ')}`);                // true
console.log(`foodList.includes('์‚ผ๊ฒน์‚ด') : ${foodList.includes('์‚ผ๊ฒน์‚ด')}`);            // false

2) push & pop

push() : ๋ฐฐ์—ด์˜ ๋งจ ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€
pop() : ๋ฐฐ์—ด์˜ ๋งจ ๋’ค์— ์š”์†Œ ์ œ๊ฑฐ

const chineseFood= ['์งœ์žฅ๋ฉด', '์งฌ๋ฝ•', '์šฐ๋™'];

console.log(`push ์ „ chineseFood : ${chineseFood}`);            // ์งœ์žฅ๋ฉด,์งฌ๋ฝ•,์šฐ๋™

chineseFood.push('ํƒ•์ˆ˜์œก');
chineseFood.push('์–‘์žฅํ”ผ');
// chineseFood.push('ํƒ•์ˆ˜์œก', '์–‘์žฅํ”ผ'); // ์ธ์ž๋ฅผ ๋‚˜์—ดํ•ด์„œ push๋„ ๊ฐ€๋Šฅ

console.log(`push ํ›„ arr :  ${chineseFood}`);                   // ์งœ์žฅ๋ฉด,์งฌ๋ฝ•,์šฐ๋™,ํƒ•์ˆ˜์œก,์–‘์žฅํ”ผ

console.log(`chineseFood.pop() : ${chineseFood.pop()}`);        // ์–‘์žฅํ”ผ   // ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด์„œ ์ œ๊ฑฐ
console.log(`chineseFood.pop() : ${chineseFood.pop()}`);        // ํƒ•์ˆ˜์œก
console.log(`chineseFood.pop() : ${chineseFood.pop()}`);        // ์šฐ๋™

console.log(`pop ํ›„ chineseFood :  ${chineseFood}`);            // ์งœ์žฅ๋ฉด,์งฌ๋ฝ•

3) unshift & shift

unshift() : ๋ฐฐ์—ด์˜ ๋งจ ์•ž์— ์š”์†Œ ์ถ”๊ฐ€
shift() : ๋ฐฐ์—ด์˜ ๋งจ ์•ž ์š”์†Œ ์ œ๊ฑฐ ํ›„ ๋ฐ˜ํ™˜

const chickenList = ['์–‘๋…์น˜ํ‚จ', 'ํ›„๋ผ์ด๋“œ', 'ํŒŒ๋‹ญ'];

console.log(`unshift ์ „ chickenList : ${chickenList}`);         // ์–‘๋…์น˜ํ‚จ,ํ›„๋ผ์ด๋“œ,ํŒŒ๋‹ญ

// ํ•˜๋‚˜ํ•˜๋‚˜ ๋„ฃ๋Š” ๊ฒƒ๊ณผ ํ•œ๋ฒˆ์— ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋„ฃ๋Š” ๊ฒƒ์€ ์ˆœ์„œ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Œ (์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋„ฃ์œผ๋ฉด ํ•œ ๋ฉ์–ด๋ฆฌ๋กœ ์•ž์— ์‚ฝ์ž…)
chickenList.unshift('๊ฐ„์žฅ์น˜ํ‚จ');
chickenList.unshift('๋งˆ๋Š˜์น˜ํ‚จ');
// chickenList.unshift('๊ฐ„์žฅ์น˜ํ‚จ', '๋งˆ๋Š˜์น˜ํ‚จ'); // ์ธ์ž๋ฅผ ๋‚˜์—ดํ•ด์„œ unshift๋„ ๊ฐ€๋Šฅ

console.log(`unshift ํ›„ chickenList : ${chickenList}`);         // ๋งˆ๋Š˜์น˜ํ‚จ,๊ฐ„์žฅ์น˜ํ‚จ,์–‘๋…์น˜ํ‚จ,ํ›„๋ผ์ด๋“œ,ํŒŒ๋‹ญ

console.log(`chickenList.shift() : ${chickenList.shift()}`);    // ๋งˆ๋Š˜์น˜ํ‚จ
console.log(`chickenList.shift() : ${chickenList.shift()}`);    // ๊ฐ„์žฅ์น˜ํ‚จ
console.log(`chickenList.shift() : ${chickenList.shift()}`);    // ์–‘๋…์น˜ํ‚จ

console.log(`shift ํ›„ chickenList : ${chickenList}`);           // ํ›„๋ผ์ด๋“œ,ํŒŒ๋‹ญ

4) concat

concat() : ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฐฐ์—ด์„ ๊ฒฐํ•ฉ

const idol1 = ['์•„์ด๋ธŒ', '์˜ค๋งˆ์ด๊ฑธ'];
const idol2 = ['ํŠธ์™€์ด์Šค', '์—์ŠคํŒŒ'];
const idol3 = ['๋ธ”๋ž™ํ•‘ํฌ', '๋ ˆ๋“œ๋ฒจ๋ฒณ'];

// ๋ฐฐ์—ด๋ช….concat(๋ฐฐ์—ด๋ช…1, ๋ฐฐ์—ด๋ช…2, ...)
const mix = idol1.concat(idol2);
const mix2 = idol3.concat(idol1, idol2);

console.log(`idol1 ๊ธฐ์ค€์œผ๋กœ idol2 ๋ฐฐ์—ด์„ concat : ${mix}`);            // ์•„์ด๋ธŒ,์˜ค๋งˆ์ด๊ฑธ,ํŠธ์™€์ด์Šค,์—์ŠคํŒŒ
console.log(`idol3 ๊ธฐ์ค€์œผ๋กœ idol1, idol2 ๋ฐฐ์—ด์„ concat : ${mix2}`);    // ๋ธ”๋ž™ํ•‘ํฌ,๋ ˆ๋“œ๋ฒจ๋ฒณ,์•„์ด๋ธŒ,์˜ค๋งˆ์ด๊ฑธ,ํŠธ์™€์ด์Šค,์—์ŠคํŒŒ

5) slice & splice

slice() : ๋ฐฐ์—ด์˜ ์š”์†Œ ์„ ํƒ ์ž˜๋ผ๋‚ด๊ธฐ
splice() : ๋ฐฐ์—ด์˜ index ์œ„์น˜์˜ ์š”์†Œ ์ œ๊ฑฐ ๋ฐ ์ถ”๊ฐ€

const front = ['HTML', 'CSS', 'JavaScript', 'jQuery'];

// slice(์‹œ์ž‘์ธ๋ฑ์Šค, ์ข…๋ฃŒ์ธ๋ฑ์Šค)
console.log(`front.slice(1, 3) : ${front.slice(1, 3)}`);                        // CSS,JavaScript
console.log(`front : ${front}`);                                                // HTML,CSS,JavaScript,jQuery

// splice(index, ์ œ๊ฑฐ์ˆ˜, ์ถ”๊ฐ€๊ฐ’1, ์ถ”๊ฐ€๊ฐ’2, ...)
console.log(`front.splice(3, 1, "React") : ${front.splice(3, 1, "React")}`);    // jQuery
console.log(`front : ${front}`);                                                // HTML,CSS,JavaScript,React

6) join

join() : ๋ฐฐ์—ด์„ ๊ตฌ๋ถ„์ž๋กœ ๊ฒฐํ•ฉํ•˜์—ฌ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜

const snackList = ['์‚ฌํƒ•', '์ดˆ์ฝœ๋ ›', '๊ปŒ', '๊ณผ์ž'];
console.log(`snackList.join() : ${snackList.join()}`);          // ์‚ฌํƒ•,์ดˆ์ฝœ๋ ›,๊ปŒ,๊ณผ์ž
console.log(`snackList.join('/') : ${snackList.join('/')}`);    // ์‚ฌํƒ•/์ดˆ์ฝœ๋ ›/๊ปŒ/๊ณผ์ž // ์›ํ•˜๋Š” ๊ตฌ๋ถ„์ž('/') ์ „๋‹ฌ

7) reverse

reverse() : : ๋ฐฐ์—ด์˜ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘์Œ (์›๋ณธ ๋ณ€ํ™”)

console.log(`[1, 2, 3, 4, 5].reverse() : ${[1, 2, 3, 4, 5].reverse()}`);    // 5,4,3,2,1

๐Ÿ‘€ ๋ฐฐ์—ด ๊ณ ์ฐจ ํ•จ์ˆ˜

๐Ÿ’โ€โ™€๏ธ ๊ณ ์ฐจ ํ•จ์ˆ˜๋ž€,
ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ ๋ฐ›๊ฑฐ๋‚˜ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

1) sort

sort() : ๋ฐฐ์—ด์„ ์ •๋ ฌ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ

let numbers = [];

for(let i = 0; i < 10; i++) {
    numbers[i] = Math.floor(Math.random() * 100) + 1; // Math.floor : ๋‚ด๋ฆผ์ฒ˜๋ฆฌ
}

console.log(`์ •๋ ฌ ์ „ numbers : ${numbers}`)

// ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์ด ๊ธฐ๋ณธ์ด๋ฉฐ ์ •๋ ฌ ํ›„ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์œ ์ง€
// ๋ฐฐ์—ด์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฌธ์ž์—ด ์ •๋ ฌ์ด ๋˜๋ฏ€๋กœ ํ•œ ์ž๋ฆฌ์ˆ˜, ์„ธ์ž๋ฆฌ ์ˆ˜๊ฐ€ ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š๊ฒŒ ์ •๋ ฌ๋˜๋Š” ๋ชจ์Šต์„ ๋ณด์ž„
// => ๋‹ค๋ฅธ ์ •๋ ฌ ๊ธฐ์ค€์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด compare ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ด์•ผํ•จ
numbers.sort();
console.log(`์ •๋ ฌ ํ›„ numbers : ${numbers}`)

// ์ˆซ์ž ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
function compare(a, b) {
    if(a > b) return 1; // 1 : ์•ž, ๋’ค ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟ”์•ผํ•œ๋‹ค๋Š” ์˜๋ฏธ
    if(a == b) return 0;
    if(a < b) return -1;
}

numbers.sort(compare);
console.log('๋งค๊ฐœ๋ณ€์ˆ˜๋กœ compare ํ•จ์ˆ˜ ์ „๋‹ฌํ•˜์—ฌ ์ˆซ์ž ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ');
console.log(numbers);

// ์ˆซ์ž ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ (ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ”๋กœ ์ „๋‹ฌ)
numbers.sort(function(a, b) { return b - a});
numbers.sort((a, b) => b - a);

console.log('์ˆซ์ž ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ');
console.log(numbers);

2) forEach

forEach() : for๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋Š” ๊ณ ์ฐจํ•จ์ˆ˜

๋ฐฐ์—ด.forEach(function(item, index, array){
        // ๋ฐฐ์—ด ์š”์†Œ ๊ฐ๊ฐ์— ์‹คํ–‰ํ•  ๊ธฐ๋Šฅ ์ž‘์„ฑ
    });
numbers = [1, 2, 3, 4, 5];

numbers.forEach(function(item, index, array){
    console.log(`item : ${item}`)
    console.log(`index : ${index}`)
    console.log(`array : ${array}`)
});

// ๊ฐ ์š”์†Œ ๋ณ„๋กœ * 10ํ•œ ๊ฐ’์„ ์ฝ˜์†”์— ์ถœ๋ ฅ
numbers.forEach(item => console.log(item * 10));

3) map

map() : ๋ฐฐ์—ด ์š”์†Œ ์ „์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„ ๋ฐ˜ํ™˜ ๊ฐ’๋“ค๋กœ ๊ตฌ์„ฑ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜

๋ฐฐ์—ด.map(function(item, index, array){
        // ๋ฐฐ์—ด ์š”์†Œ ๊ฐ๊ฐ์— ๋ฐ˜ํ™˜ํ•  ์ƒˆ๋กœ์šด ๊ฐ’
    }) 
const types = [true, 1, 'text'].map(item => typeof item); // item๋“ค์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ฐฐ์—ด๋กœ
console.log(types);  // [ 'boolean', 'number', 'string' ]

const lengths = ['apple๐ŸŽ', 'banana๐ŸŒ', 'cat๐Ÿฑ', 'dog๐Ÿถ', 'egg๐Ÿณ'].map(item => item.length);
console.log(lengths); // [ 7, 8, 5, 5, 5 ] => ์ด๋ชจ์ง€๋Š” length๊ฐ€ 2 !

// Array.prototype.filter : ๋ฐฐ์—ด ์š”์†Œ ์ „์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„ ๋ฐ˜ํ™˜ ๊ฐ’์ด true์ธ ์š”์†Œ๋กœ๋งŒ ๊ตฌ์„ฑ ๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฐ˜ํ™˜
const odds = numbers.filter(item => item % 2); // 1์ด๋ผ๋Š” ๊ฐ’๋งŒ true?
console.log(odds); // [ 1, 3, 5 ] => ํ™€์ˆ˜๋กœ๋งŒ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด ์ถœ๋ ฅ

4) reduce

reduce() : ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•˜์—ฌ ์ด์ „์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์‹คํ–‰ ๋ฐ˜ํ™˜๊ฐ’์„ ์ „๋‹ฌํ•˜์—ฌ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜
(์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ๋™์ž‘ํ•  ๋•Œ return ํ•˜๋Š” ๊ฐ’์ด ๋‹ค์Œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ)

previouseValue : ์ด์ „ ์ฝœ๋ฐฑ์˜ ๋ฐ˜ํ™˜๊ฐ’
currentValue : ๋ฐฐ์—ด ์š”์†Œ์˜ ๊ฐ’
currentIndex : ์ธ๋ฑ์Šค
array : ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๋ฐฐ์—ด

numbers.reduce(function(previouseValue, currentValue, currentIndex, array){
    console.log(`previouseValue : ${previouseValue}`); // ์ด์ „ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’
    console.log(`currentValue : ${currentValue}`);
    console.log(`currentIndex : ${currentIndex}`);
    console.log(`array : ${array}`);
});

// ํ•ฉ์‚ฐ
const sum = [3, 4, 5, 6].reduce(function(previouseValue, currentValue){
    return previouseValue + currentValue; 
});
console.log(`sum : ${sum}`); // sum : 18
/* ๋‚ด๊ฐ€ ์“ฐ๋Š” ํ•ด์„ค
    1st turn : 0(pv) + 3(cv) = 3(๋‹ค์Œ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ˜ํ™˜) 
    2nd turn : 3(pv) + 4(cv) = 7(๋‹ค์Œ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ˜ํ™˜)
    3rd turn : 7(pv) + 5(cv) = 12(๋‹ค์Œ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ˜ํ™˜)
    4th turn : 12(pv) + 6(cv) = 18(๋‹ค์Œ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ˜ํ™˜)
    => ๋”ฐ๋ผ์„œ ๋งˆ์ง€๋ง‰ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’์ธ 18์ด ์ตœ์ข… ๋ฐ˜ํ™˜ 
*/
 
// ์ตœ๋Œ€๊ฐ’ ์ทจ๋“
const max = [3, 4, 5, 6].reduce(function(pre, cur){
    return pre > cur ? pre : cur;
});
console.log(`max : ${max}`); // max : 6
/* ๋‚ด๊ฐ€ ์“ฐ๋Š” ํ•ด์„ค
    1st turn : 3(pv) > 4(cv) ? 3 : 4; (false์ด๋ฏ€๋กœ 4๋ฅผ ๋‹ค์Œ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ˜ํ™˜)
    2nd turn : 4(pv) > 5(cv) ? 4 : 5; (false์ด๋ฏ€๋กœ 5๋ฅผ ๋‹ค์Œ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ˜ํ™˜)
    3rd turn : 5(pv) > 6(cv) ? 5 : 6; (false์ด๋ฏ€๋กœ 6๋ฅผ ๋‹ค์Œ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ˜ํ™˜)
    => ๋”ฐ๋ผ์„œ ๋งˆ์ง€๋ง‰ ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’์ธ 6์ด ์ตœ์ข… ๋ฐ˜ํ™˜ 
*/

5) some

some() : ๋ฐฐ์—ด ๋‚ด ์ผ๋ถ€ ์š”์†Œ๊ฐ€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ boolean์œผ๋กœ ๋ฐ˜ํ™˜

// ๋ฐฐ์—ด ๋‚ด ์š”์†Œ ์ค‘ 10๋ณด๋‹ค ํฐ ๊ฐ’์ด 1๊ฐœ ์ด์ƒ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
let result = [1, 5, 3, 2, 4].some(item => item > 10);
console.log(`result : ${result}`) // result : false

// ๋ฐฐ์—ด ๋‚ด ์š”์†Œ ์ค‘ 3๋ณด๋‹ค ํฐ ๊ฐ’์ด 1๊ฐœ ์ด์ƒ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
result = [1, 5, 3, 2, 4].some(item => item > 3);
console.log(`result : ${result}`) // result : true (ํ•˜๋‚˜๋ผ๋„ true๋ฉด ์ „์ฒด์ ์ธ ๊ฒฐ๊ณผ๊ฐ€ true)

6) every

every() : ๋ฐฐ์—ด ๋‚ด ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ™•์ธํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ boolean์œผ๋กœ ๋ฐ˜ํ™˜

// ๋ฐฐ์—ด ๋‚ด ์š”์†Œ ๋ชจ๋“  ๊ฐ’์ด 3๋ณด๋‹ค ํฐ์ง€ ํ™•์ธ
result = [1, 5, 3, 2, 4].every(item => item > 3); 
console.log(`result : ${result}`) // result : false (ํ•˜๋‚˜๋ผ๋„ false๋ฉด ์ „์ฒด์ ์ธ ๊ฒฐ๊ณผ๊ฐ€ false)

// ๋ฐฐ์—ด ๋‚ด ์š”์†Œ ๋ชจ๋“  ๊ฐ’์ด 0๋ณด๋‹ค ํฐ์ง€ ํ™•์ธ
result = [1, 5, 3, 2, 4].every(item => item > 0);
console.log(`result : ${result}`) // result : true

7) find & findIndex

find() : ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•˜์—ฌ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ฉฐ ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜. ์ฐธ์ธ ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด undefined ๋ฐ˜ํ™˜
findIndex() : ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•˜์—ฌ ์ธ์ž๋กœ ์ฃผ์–ด์ง„ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ทธ ๊ฒฐ๊ณผ๊ฐ€ ์ฐธ์ธ ์ฒซ ๋ฒˆ์žฌ ์š”์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜. ์ฐธ์ธ ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด -1 ๋ฐ˜ํ™˜

const students = [
    { name : '์‹ ์งฑ๊ตฌ', score : 90},
    { name : '์‹ ์งฑ์•„', score : 80},
    { name : 'ํฐ๋‘ฅ์ด', score : 70},
    { name : '์‹ ์งฑ๊ตฌ', score : 20}
];

result = students.find(item => item.name === '์‹ ์งฑ๊ตฌ');
console.log(result);    // { name: '์‹ ์งฑ๊ตฌ', score: 90 } (20์  ์งฑ๊ตฌ๊ฐ€ ์•„๋‹Œ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์ธ 90์  ์งฑ๊ตฌ๋ฅผ ๋ฐ˜ํ™˜)
result = students.findIndex(item => item.name === '์‹ ์งฑ๊ตฌ');
console.log(result);    // 0

// ์กด์žฌํ•˜์ง€ ์•Š์„ ๋•Œ,
result = students.find(item => item.name === '์‹ ํ˜•๋งŒ');
console.log(result);    // undefined
result = students.findIndex(item => item.name === '์‹ ํ˜•๋งŒ');
console.log(result);    // -1

// find, findIndex๋Š” ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐพ์œผ๋ฉด ๋” ์ด์ƒ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๊ณ  ํ•˜๋‚˜์˜ ์š”์†Œ, ์ธ๋ฑ์Šค๋งŒ ๋ฐ˜ํ™˜
// ๋งŒ์•ฝ 60์  ์ด์ƒ์ธ ํ•™์ƒ๋“ค์„ ์ฐพ๊ณ  ์‹ถ๋‹ค๋ฉด?
// filter๋Š” ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ true์ธ ๋ฐฐ์—ด ์š”์†Œ์˜ ๊ฐ’๋งŒ์„ ์ถ”์ถœํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜

result = students.filter(item => item.score >= 60);
console.log(result);    // ๋ชจ๋“  60์  ์ด์ƒ์˜ ํ•™์ƒ๋“ค ์ถœ๋ ฅ

profile
Tiny little habits make me

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