Array

kirin.logยท2021๋…„ 2์›” 10์ผ
1

๐ŸŽฒ Array

์—ฐ๊ด€๋œ ๋ฐ์ดํ„ฐ๋“ค์„ ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์œผ๋กœ ๋ฌถ์–ด์„œ ํšจ์œจ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.

๐Ÿ‘‰ ๋ฐฐ์—ด์— ์ €์žฅ๋˜๋Š” ์š”์†Œ(elements)๋Š” ์ˆœ์ฐจ์ (0๋ถ€ํ„ฐ ์‹œ์ž‘)์œผ๋กœ ์ €์žฅ๋˜๊ณ  ๊ณ ์œ ํ•œ index๊ฐ’์„ ๊ฐ–๋Š”๋‹ค.
๐Ÿ‘‰ ๋ฐฐ์—ด์˜ ํŠน์ • element์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” arrayValue[index]์˜ ํ˜•์‹์œผ๋กœ ํ•œ๋‹ค.
๐Ÿ‘‰ ๋ฐฐ์—ด์€ ์š”์†Œ index๋กœ ๋ฌธ์ž์—ด(์—ฐ๊ด€ ๋ฐฐ์—ด)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๋ฌด์กฐ๊ฑด ์ •์ˆ˜๋งŒ ํ—ˆ์šฉ
๐Ÿ‘‰ ์†์„ฑ ์ ‘๊ทผ์ž(์  ๋˜๋Š” ๊ด„ํ˜ธ ํ‘œ๊ธฐ๋ฒ•์œผ๋กœ ๊ฐ์ฒด์˜ ์†์„ฑ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๋ฐฉ์‹)๋ฅผ ์‚ฌ์šฉํ•ด ์ •์ˆ˜ ์™ธ ํ‚ค์— ์ ‘๊ทผํ•  ๊ฒฝ์šฐ, ๋ฐฐ์—ด ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด ๊ฐ์ฒด์˜ ์†์„ฑ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ํšŒ์ˆ˜ํ•ด์•ผํ•จ

โ— ์—ฐ๊ด€๋ฐฐ์—ด
์—ฐ๊ด€ ๋ฐฐ์—ด(associative array)์€ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ํ•˜๋‚˜๋กœ, ํ‚ค ํ•˜๋‚˜์™€ ๊ฐ’ ํ•˜๋‚˜๊ฐ€ ์—ฐ๊ด€๋˜์–ด ์žˆ์œผ๋ฉฐ ํ‚ค๋ฅผ ํ†ตํ•ด ์—ฐ๊ด€๋˜๋Š” ๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ์—ฐ์ƒ ๋ฐฐ์—ด, ๊ฒฐํ•ฉํ˜• ๋ฐฐ์—ด, ๋งต(map), ์‚ฌ์ „(dictionary)์œผ๋กœ ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค.


๐Ÿธ Array ์„ ์–ธ ๋ฐฉ๋ฒ•

Array๋ฅผ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ• 3๊ฐ€์ง€ (a, b, c๊ฐ€ ๊ฐ™๋‹ค)

var a = new Array(1,2,3);   // a ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž… ์ƒ์„ฑ(new ์ƒ์„ฑ์ž)
var b = [1,2,3];            // ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ๋ฐฐ์—ด์„ ๋‚˜์—ดํ•ด ์ค€๋‹ค
var c = new Array();        // ์ƒ์„ฑ์ž ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ pushํ•˜์—ฌ ์š”์†Œ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค
c.push(1);    //1
c.push(2);    //2
c.push(3);    //3


// ์ฒซ๋ฒˆ์งธ ์ธ์ž์˜ ํ˜•์‹์— ๋”ฐ๋ฅธ ์ฐจ์ด
var a = new Array(5);
console.log(a);   //[undefined,undefined,undefined,undefined,undefined]
            // ๊ฐ’์ด ์—†๋Š” 5๊ฐœ์˜ ์›์†Œ๋ฅผ ํฌํ•จํ•œ array๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์ค˜์•ผ ํ•จ
var b = new Array('5');
console.log(b);   // string object 5๋ฅผ ์›์†Œ๋กœ ํ•˜๋Š” array๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ["5"]
var c = new Array(new Number(5))
console.log(c); // number object 5๋ฅผ ์›์†Œ๋กœ ํ•˜๋Š” array๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. [5] 

๐Ÿธ Array ์†์„ฑ ์ถ”๊ฐ€ & ์š”์†Œ ์ถ”๊ฐ€

associative array(์—ฐ๊ด€๋ฐฐ์—ด)๋ฅผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๋ฐฐ์—ด์ด ์•„๋‹ˆ๋ผ object๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค.


// ์†์„ฑ(property) ์ถ”๊ฐ€
codingeverybody = new Array();
codingeverybody['html'] = '์›น๋ฌธ์„œ๋ฅผ ๋งŒ๋“ ๋‹ค';
codingeverybody['css'] = 'html์„ ๊พธ๋ฉฐ์ค€๋‹ค';
codingeverybody['javascript'] = 'html์„ ๋™์ ์œผ๋กœ ์ œ์–ดํ•œ๋‹ค';
codingeverybody['php'] = 'html์„ ์„œ๋ฒ„์ธก์—์„œ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค';

console.log(codingeverybody['html']);   // string, ์›น๋ฌธ์„œ๋ฅผ ๋งŒ๋“ ๋‹ค
console.log(codingeverybody['css']);    // string, html์„ ๊พธ๋ฉฐ์ค€๋‹ค

// ์š”์†Œ ์ถ”๊ฐ€( = push)
codingeverybody.push(1);
codingeverybody.push(2);

console.log(codingeverybody);              // array, [1,2]
console.log(codingeverybody.length);       // number, 2
// codingeverybody๋Š” array object์ด์ง€๋งŒ, ๋™์‹œ์— object์ด๊ธฐ๋„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 'html', 'css', 'javascript', 'php'๋Š” object codingeverybody์˜ ์›์†Œ๊ฐ€ ์•„๋‹ˆ๋ผ property(์†์„ฑ)์œผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
// ๊ทธ๋ž˜์„œ ์œ„์˜ ์ฝ”๋“œ์—์„œ codingeverybody.length ๊ฐ€ 2๊ฐ€ ๋œ๋‹ค. (push๋กœ ์ถ”๊ฐ€๋œ 1,2๋งŒ ์นด์šดํŠธํ•˜์—ฌ ์ด 2   

โ— ๋ฐฐ์—ด์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์œ ์ผํ•œ ๋ฐฉ๋ฒ•์€ push ๋“ฑ์„ ํ†ตํ•ด ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ!!! (์†์„ฑ ์ถ”๊ฐ€์™€ ๋‹ค๋ฅด๋‹ค)
โ— ๋ฐฐ์—ด์˜ ๊ธธ์ด(์š”์†Œ์˜ ๊ฐฏ์ˆ˜)๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ .length๋ฅผ ํ™œ์šฉํ•œ๋‹ค.
โžก ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ฐพ์œผ๋ ค๋ฉด ๋ฐฐ์—ด์ด๋ฆ„[0]
โžก ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ฐพ์œผ๋ ค๋ฉด ๋ฐฐ์—ด์ด๋ฆ„[๋ฐฐ์—ด์ด๋ฆ„.length -1]

const arr = ['first', 'second', 'third', 'forth', 'fifth']

console.log(arr[0])  // 'first'
console.log(arr[arr.length-1])  // 'fifth'
// arr[index]๋Š” ์š”์†Œ๋ฅผ ์ฐพ๋Š”๋‹ค. ๋Œ€๊ด„ํ˜ธ ์•ˆ์— arr.length-1์„ ํ•˜๋ฉด ๋งˆ์ง€๋ง‰ index๊ฐ€ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์—
// ๊ฒฐ๊ตญ arr[index]๊ฐ€ ๋˜๋ฉฐ, ๋งˆ์ง€๋ง‰ index์˜ ์š”์†Œ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค


// ๋ฐฐ์—ด์˜ ์›์†Œ์— ์ ‘๊ทผ ํ•˜๋Š” ๋ฐฉ๋ฒ•  >>  indexํ™œ์šฉ
let a = new Array(1,2,3);
console.log(a[0]); // 1
console.log(b[1]); // 2
console.log(c[2]); // 3

๐Ÿธ Array ์™€ ๋ฐ˜๋ณต๋ฌธ

let arr = new Array(1,2,3);

// for ๋ฌธ ํ™œ์šฉ
for(let i = 0; i < arr.length; i++){
    console.log(arr[i]);    // 1, 2, 3
}
// arr์˜ ์ธ๋ฑ์Šค๋กœ i๋ฅผ 0๋ถ€ํ„ฐ 2๊นŒ์ง€ ์ˆœ์ฐจ์ ์œผ๋กœ ๋Œ€์ž…ํ•œ ํ›„์— arr[index]์ถœ๋ ฅ


// for ... of ํ™œ์šฉ
for( let el of arr ){
    console.log(el);       // 1, 2, 3
}


// forEach ํ™œ์šฉ
arr.forEach( el => console.log(el))
// ํ•ด๋‹น ๋ฐฐ์—ด ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ๊ฐ’๋งˆ๋‹ค ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.
// ์ฆ‰ ๋ฐฐ์—ด์˜ ๋ชจ๋“  ๊ฐ’์ด ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ๋“ค์–ด๊ฐ„ ๋’ค ํ•ด๋‹น ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์›๋ž˜ ๋ฐฐ์—ด(arr)์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ


๐ŸŽฒ Array ๋ฉ”์„œ๋“œ

โœ… ๋ฐฐ์—ด์„ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฉ”์„œ๋“œ

๐Ÿ˜ concat

๐Ÿ‘‰ 2๊ฐœ ์ด์ƒ์˜ ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๊ฒฐํ•ฉํ•ด์„œ returnํ•œ๋‹ค.
(2๊ฐœ ์ด์ƒ์˜ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด concat ํ•จ์ˆ˜๋ฅผ ์ค‘์ฒฉ์‹œ์ผœ์„œ ์‚ฌ์šฉ)
๐Ÿ‘‰ ๋‘ ๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž(||)๋Š” concatํ•จ์ˆ˜์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜. (๊ฐ’ || ๊ฐ’ || ๊ฐ’ ์œผ๋กœ ์‚ฌ์šฉ)
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array1.concat(array2);

//concatํ•จ์ˆ˜๋ฅผ ๋ฉ”์†Œ๋“œ๋กœ ํ™œ์šฉํ•˜์—ฌ ๋ฐฐ์—ด return
let a = new Array(1,2,3);
let b = new Array(4,5,6);
let c = new Array(7,8,9);

a.concat(b,c); 
// [1,2,3,4,5,6,7,8,9]



//concatํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด return
let a = 'apple'
a.concat(' is fresh,' , 'good');
// apple is fresh, good



//์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž(||)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ concat๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ return
let a = new Array(1,2,3);
let b = new Array(4,5,6);
let c = new Array(7,8,9);

a||b||c; // [1,2,3,4,5,6,7,8,9]

๐Ÿ‡ join

๐Ÿ‘‰ ๋ฐฐ์—ด์˜ ์›์†Œ๋ฅผ ๊ฒฐํ•ฉํ•ด์„œ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ ๋‹ค. ๋ฐฐ์—ด์„ String์œผ๋กœ ๋ณ€ํ™˜
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.join(separator๊ตฌ๋ถ„์ž);
๐Ÿ‘‰ ๋ฐ˜ํ™˜: ๋ฌธ์ž์—ด

// separator(๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๊ตฌ๋ถ„ํ•  ๊ตฌ๋ถ„์ž๋ฅผ ์ง€์ •) ์˜ˆ์‹œ
let weather = ['๋ฐ”๋žŒ', '๋น„', '๋ถˆ'];

let join1 = weather.join();      // ๊ธฐ๋ณธ ๊ตฌ๋ถ„์ž. ๊ฒฐ๊ณผ๊ฐ’์€ ์ปด๋งˆ(,)๋กœ ๊ตฌ๋ถ„๋จ
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: '๋ฐ”๋žŒ,๋น„,๋ถˆ'

let join2 = weather.join(', ');  // "์ปด๋งˆ ๋„์–ด์“ฐ๊ธฐ"๋กœ ํ‘œํ˜„ 
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: '๋ฐ”๋žŒ, ๋น„, ๋ถˆ'

let join3 = weather.join(' + '); // "๋„์–ด์“ฐ๊ธฐ ํ”Œ๋Ÿฌ์Šค ๋„์–ด์“ฐ๊ธฐ"๋กœ ํ‘œํ˜„ 
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: '๋ฐ”๋žŒ + ๋น„ + ๋ถˆ'

let join4 = weather.join('');    // ๊ณต๋ฐฑ์—†์ด ํ‘œํ˜„ 
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: '๋ฐ”๋žŒ๋น„๋ถˆ'

๐Ÿ‡ split

๐Ÿ‘‰ ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด์•ˆ์— ์š”์†Œ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ๋‹ด๋Š”๋‹ค. String์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
๐Ÿ‘‰ ๋ฌธ๋ฒ•: string.split(separator๊ตฌ๋ถ„์ž);
๐Ÿ‘‰ ๋ฐ˜ํ™˜: ๋ฐฐ์—ด

// separator(๋ฌธ์ž์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๊ตฌ๋ถ„ํ•  ๊ตฌ๋ถ„์ž๋ฅผ ์ง€์ •) ์˜ˆ์‹œ
let weather = '๋ฐ”๋žŒ, ๋น„, ๋ถˆ';

let split1 = weather.split();      // ๊ตฌ๋ถ„์ž๊ฐ€ ์—†์œผ๋ฉด ์ „์ฒด๋ฅผ ํ•œ ๋ฒˆ์— ๋ฐฐ์—ด๋กœ ๋‹ด์Œ
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: [ '๋ฐ”๋žŒ, ๋น„, ๋ถˆ' ]

let split2 = weather.split(' ');  // ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ชผ๊ฐœ์„œ ๋ฐฐ์—ด์— ๋‹ด์Œ
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: [ '๋ฐ”๋žŒ,', '๋น„,', '๋ถˆ' ]

let split3 = weather.split(' + '); // ์ฝค๋งˆ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ชผ๊ฐœ์„œ ๋ฐฐ์—ด์— ๋‹ด์Œ
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: [ '๋ฐ”๋žŒ', ' ๋น„', ' ๋ถˆ' ]

โ— string.split( separator, limit )
โžก ๊ด„ํ˜ธ ์•ˆ์˜ ์ฒซ ๋ฒˆ์งธ ์ž๋ฆฌ๋Š” ๊ตฌ๋ถ„์ž๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ํ•˜๋ฉฐ ํ•„์ˆ˜์‚ฌํ•ญ์ด๋‹ค.
โžก ๋‘๋ฒˆ์งธ ์ž๋ฆฌ๋Š” ์ชผ๊ฐœ์–ด ๋‹ด์„ ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋œปํ•œ๋‹ค.

weather.split(',', 2)
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: [ '๋ฐ”๋žŒ', ' ๋น„' ]
// 2๋ฒˆ์งธ index ์ด์ „๊นŒ์ง€ ์ž˜๋ผ์„œ ๋‹ด๋Š”๋‹ค

โœ… ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€/์ œ๊ฑฐ ํ•˜๋Š” ๋ฉ”์„œ๋“œ

๐Ÿˆ pop / push

๐Ÿ‘‰ pop
array.pop()
๋ฐฐ์—ด์—์„œ ๋งˆ์ง€๋ง‰ ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•œ ํ›„์— ์ด๋ฅผ return ( <-> shift)

๐Ÿ‘‰ push
array.push()
๋ฐฐ์—ด์—์„œ ๋งˆ์ง€๋ง‰์— ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„์— return ( <-> unshift)
๋ฌธ๋ฒ• โžก array.push ( ์ถ”๊ฐ€ํ•  element1, ์ถ”๊ฐ€ํ•  element2 . . . ์ถ”๊ฐ€ํ•  element3 )
๋ฐ˜ํ™˜๊ฐ’ โžก ์›์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ ์ดํ›„์— ๋ฐฐ์—ด์˜ ์›์†Œ ์ˆ˜(length)

pop ๊ณผ push ๋น„๊ต

// pop
let jobs = ['programmer', 'designer', 'teacher']

console.log(jobs);          // ['programmer', 'designer', 'teacher']
console.log(jobs.pop());    // ['teacher']
console.log(jobs);          // ['programmer', 'designer']  ๋งˆ์ง€๋ง‰ ์š”์†Œ๊ฐ€ ์ œ๊ฑฐ๋œ ๋ฐฐ์—ด์ด ์ถœ๋ ฅ

let job1 = jobs.pop();   // jobs.pop(); ํ˜ธ์ถœ์„ ๋ณ€์ˆ˜(job1)๋กœ ํ•ด์ค„ ๋ฟ
job1;          // ['teacher']



// push
let jobs = ['programmer', 'designer', 'teacher'];
let job2 = jobs.push('singer');

console.log(jobs);     // ['programmer', 'designer', 'teacher', 'singer']
console.log(job2);     // 4 (์ด ์›์†Œ ๊ฐฏ์ˆ˜๊ฐ€ 4๊ฐœ๊ฐ€ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ)

๐Ÿˆ shift / unshift

๐Ÿ‘‰ shift
array.shift()
๋ฐฐ์—ด์—์„œ ์ฒซ๋ฒˆ์งธ ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•œ ํ›„์— ์ด๋ฅผ return ( <-> pop)

๐Ÿ‘‰ unshift
array.unshift()
๋ฐฐ์—ด์—์„œ ์ฒซ๋ฒˆ์งธ์— ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ ํ›„์— return ( <-> push)
๋ฌธ๋ฒ•: array.unshift(element1, element2...element3)
๋ฐ˜ํ™˜๊ฐ’: ์›์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ ์ดํ›„์— ๋ฐฐ์—ด์˜ ์›์†Œ ์ˆ˜(length)

shift ์™€ unshift ๋น„๊ต

// shift
let jobs = ['programmer', 'designer', 'teacher'];
console.log(jobs.shift());   // ['programmer']
console.log(jobs);           // ['designer', 'teacher'] 

let job3 = jobs.shift();    // jobs.shift(); ํ˜ธ์ถœ์„ ๋ณ€์ˆ˜๋กœ ํ•ด์ค„ ๋ฟ
console.log(job3);          // ['programmer'] 


// unshift
let jobs = ['programmer', 'designer', 'teacher'];
console.log(jobs.unshift('singer'));   // 4
console.log(jobs);                     // ['programmer', 'designer', 'teacher', 'singer']

let job4 = jobs.unshift('singer');     // jobs.unshift(); ํ˜ธ์ถœ์„ ๋ณ€์ˆ˜(job4)๋กœ ํ•ด์ค„ ๋ฟ
console.log(job4);         // 4

๐Ÿˆ slice

๐Ÿ‘‰ ๋ฐฐ์—ด์˜ ํŠน์ • start index๋ถ€ํ„ฐ ํŠน์ • end index๊นŒ์ง€ ์ž˜๋ผ๋‚ด๋Š” ๋ฉ”์„œ๋“œ
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.slice(start, end)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: ์ƒˆ๋กœ์šด array ์ถœ๋ ฅ

โ— end index๋Š” ํ•ด๋‹น ์ธ๋ฑ์Šค ์ด์ „๊นŒ์ง€๋กœ ๋ฒ”์œ„๋ฅผ ์ œํ•œํ•œ๋‹ค.
์ฆ‰, end index๋ฅผ 2๋กœ ์ง€์ •ํ•˜๋ฉด, index 1๊นŒ์ง€ slice ๋œ๋‹ค. (end index ์ž์ฒด๋Š” ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค)

let jobs = ['programmer', 'designer', 'teacher', 'singer'];

jobs.slice(0,2);     // [ 'programmer', 'designer' ]
jobs.slice(1,3);     // [ 'designer', 'teacher' ]

โœ… ๋ฐฐ์—ด ์š”์†Œ์˜ ์ˆœ์„œ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฉ”์„œ๋“œ

๐Ÿป sort

๐Ÿ‘‰ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ์ •์˜ํ•˜๋Š” ํ•จ์ˆ˜
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.sort(sortfunc)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: array, ์ •๋ ฌ๋œ ๋ฐฐ์—ด, ์ž๊ธฐ ์ž์‹ ์— ๋Œ€ํ•œ ๋ ˆํผ๋Ÿฐ์Šค ๊ฐ’

๐Ÿช ์ •๋ ฌ๊ธฐ์ค€

  • ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค.
  • ์ˆซ์ž์˜ ๊ฒฝ์šฐ ์•”์‹œ์ ์œผ๋กœ ๋ฌธ์ž์—ด๋กœ ํ˜•๋ณ€ํ™˜ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆซ์ž 10์ด ์ˆซ์ž 2๋ณด๋‹ค ๋จผ์ € ๋‚˜์˜จ๋‹ค.
  • sortfunc ๋กœ ๋น„๊ต ๋Œ€์ƒ์ธ ๋‘๊ฐœ์˜ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜๋Š”๋ฐ, ๋ฆฌํ„ด ๊ฐ’์— ๋”ฐ๋ผ์„œ ์„ ํ›„๋ฅผ ํŒ๋‹จํ•œ๋‹ค.
arr.sort( (a,b) => b-a )
let numbers = [9,8,7,6,5,4,3,2,1];
console.log(numbers.sort()); // 1,2,3,4,5,6,7,8,9 (์ˆซ์ž๋Š” 1๋ถ€ํ„ฐ)
console.log(numbers); 
// ์ถœ๋ ฅ๊ฒฐ๊ณผ : [1,2,3,4,5,6,7,8,9] 


let numbers = [20,10,9,8,7,6,5,4,3,2,1];
console.log(numbers.sort());  
// ์ถœ๋ ฅ๊ฒฐ๊ณผ : [1,10,2,20,3,4,5,6,7,8,9] 
// ์•”์‹œ์ ์œผ๋กœ ์›์†Œ๋ฅผ ๋ฌธ์ž๋กœ ํ˜•๋ณ€ํ™˜ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 10์ด 1๋’ค์— ์˜จ๋‹ค.


const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// ["Dec", "Feb", "Jan", "March"]  ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ


const array = [1, 30, 4, 21, 100000];
array.sort();
console.log(array);
// [1, 100000, 21, 30, 4]


let numbers = [4, 2, 5, 1, 3];
numbers.sort( (a, b) => a - b );
// [1, 2, 3, 4, 5]
numbers.sort( (a, b) => b - a );
// [5, 4, 3, 2, 1]
numbers.sort( (a, b) => 0 )
// [4, 2, 5, 1, 3]


let stringArray = ['Blue', 'Humpback', 'Beluga'];
let numberArray = [40, 1, 5, 200];
let numericStringArray = ['80', '9', '700'];
let mixedNumericArray = ['80', '9', '700', 40, 1, 5, 200];

function compareNumbers(a, b) {
  return a - b;
}

console.log(stringArray.join());  // Blue,Humpback,Beluga
console.log(stringArray.sort());  // Beluga,Blue,Humpback

console.log(numberArray.join());  // 40,1,5,200
console.log(numberArray.sort());  // 1,200,40,5
console.log(numberArray.sort(compareNumbers));  // 1,5,40,200

console.log(numericStringArray.join());  // 80,9,700
console.log(numericStringArray.sort());  // 700,80,9
console.log(numericStringArray.sort(compareNumbers));  // 9,80,700

console.log(mixedNumericArray.join());  // 80,9,700,40,1,5,200
console.log(mixedNumericArray.sort());  // 1,200,40,5,700,80,9
console.log(mixedNumericArray.sort(compareNumbers));  // 1,5,9,40,80,200,700

๐Ÿป reverse

๐Ÿ‘‰ ์ •๋ ฌ ์ˆœ์„œ๋ฅผ ๊ฑฐ๊พธ๋กœ ๋งŒ๋“œ๋Š” ๋ฉ”์„œ๋“œ
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.reverse()
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: ๊ธฐ์กด array๊ฐ€ ๋ฐ”๋€Œ์–ด์„œ ์ถœ๋ ฅ

let numbers = [9,8,7,6,5,4,3,2,1];
console.log(numbers.reverse()); // 1,2,3,4,5,6,7,8,9 (์ˆซ์ž๋Š” 1๋ถ€ํ„ฐ)
console.log(numbers); 
// ์ถœ๋ ฅ๊ฒฐ๊ณผ : [1,2,3,4,5,6,7,8,9] 
// ์›๋ž˜ ๋ฐฐ์—ด์ธ numbers์˜ ๊ฐ’์ด ๋ฐ”๋€Œ์–ด์„œ ์ถœ๋ ฅ๋œ๋‹ค. (์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค)

โœ… ๋ฐฐ์—ด์˜ ์š”์†Œ(index)๋ฅผ ์ฐพ๋Š” ๋ฉ”์„œ๋“œ

๐Ÿน includes

๐Ÿ‘‰ ๋ฐฐ์—ด ๋‚ด ํŠน์ • ์š”์†Œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.includes(element)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: true / false ๋กœ ๋ฐ˜ํ™˜

let fruits = ['apple', 'banana', 'grape', 'mango']

console.log(fruits.includes('apple'));    // true
console.log(fruits.includes('orange'));   // false

๐Ÿน indexOf

๐Ÿ‘‰ ๋ฐฐ์—ด ๋‚ด ํŠน์ • ์š”์†Œ์˜ index๋ฅผ ๋ฐ˜ํ™˜
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.indexOf(element)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: ์š”์†Œ์˜ index๊ฐ’ ๋ฐ˜ํ™˜

let fruits = ['apple', 'banana', 'grape', 'mango']

console.log(fruits.indexOf('apple'));    // 0
console.log(fruits.indexOf('orange'));   // -1

โ— ๋ฐฐ์—ด ์š”์†Œ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’์ด๋ฉด -1 ์ถœ๋ ฅ

๐Ÿน lastIndexOf

๐Ÿ‘‰ ๋ฐฐ์—ด ๋‚ด ํŠน์ • ์š”์†Œ์˜ ๋งˆ์ง€๋ง‰ index๋ฅผ ๋ฐ˜ํ™˜
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.lastIndexOf(element)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: ๋งˆ์ง€๋ง‰ ์š”์†Œ์˜ index๊ฐ’ ๋ฐ˜ํ™˜ (๊ฐ™์€ ์š”์†Œ๊ฐ€ 2๊ฐœ ๋“ค์–ด์žˆ์„ ๊ฒฝ์šฐ, ๋งˆ์ง€๋ง‰ ์š”์†Œ์˜ index์ถœ๋ ฅ)
โ— ๋ฐฐ์—ด ์š”์†Œ์— ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ’์ด๋ฉด -1 ์ถœ๋ ฅ

let fruits = ['apple', 'banana', 'grape', 'mango', 'apple']

console.log(fruits.indexOf('apple'));    // 0
// apple ์š”์†Œ๊ฐ€ 2๊ฐœ ์žˆ์„ ๊ฒฝ์šฐ, ์ฒซ ๋ฒˆ์งธ index๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค
console.log(fruits.lastIndexOf('apple'));   // 4
// lastIndexOf๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋งˆ์ง€๋ง‰ ์š”์†Œ ๋˜๋Š” ๊ฐ™์€ ์š”์†Œ ์ค‘ ๋งˆ์ง€๋ง‰ ๋ฒˆ์งธ ์š”์†Œ์˜ index๋ฅผ ์ถœ๋ ฅ

โ— ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ฐฐ์—ด๋‚ด ๋˜‘๊ฐ™์€ ์š”์†Œ์˜ ์ฒซ ๋ฒˆ์งธ index๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์œผ๋ฉด lastIndexOf๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค

๐Ÿน find

๐Ÿ‘‰ ๋ฐฐ์—ด ๋‚ด ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์„œ, ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ(true์ธ ์š”์†Œ)๋ฅผ ์ถœ๋ ฅ
= ๋ฐฐ์—ด ๋‚ด ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์š”์†Œ ํ•˜๋‚˜ ํ•˜๋‚˜ ์ ‘๊ทผํ•œ ๋’ค, ์š”์†Œ๊ฐ€ true์ผ ๋•Œ returnํ•œ๋‹ค
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.find( el => ์กฐ๊ฑด)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: ์กฐ๊ฑด์— ์ถฉ์กฑํ•œ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ ์ถœ๋ ฅ

class Student {
  constructor (name, age, enrolled, score) {
    this.name = name;
    this.age = age;
    this.enrolled = enrolled;
    this.score = score;
  }
}


const students = [
  new Student ('A', 29, true, 45),
  new Student ('B', 28, false, 80),
  new Student ('C', 30, true, 90),
  new Student ('D', 40, false, 66),
  new Student ('E', 18, true, 88),
];


// ์ ์ˆ˜๊ฐ€ 90์  ์ด์ƒ์ธ ํ•™์ƒ ์ฐพ๊ธฐ
const result = students.find( student => student.score===90);
console.log(result)

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: Student {
//            name: 'C',
//            age: 30,
//            enrolled: true,
//            score: 90,
//            __proto__: Student { constructor: ฦ’ Student() }
//          }

๐Ÿน filter

๐Ÿ‘‰ ๋ฐฐ์—ด ๋‚ด ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์„œ, ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ(true์ธ ์š”์†Œ)๋ฅผ ์ถœ๋ ฅ
= ๋ฐฐ์—ด ๋‚ด ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์š”์†Œ ํ•˜๋‚˜ ํ•˜๋‚˜ ์ ‘๊ทผํ•œ ๋’ค, ์š”์†Œ๊ฐ€ true์ผ ๋•Œ returnํ•œ๋‹ค
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.filter( el => ์กฐ๊ฑด)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: ์กฐ๊ฑด์— ์ถฉ์กฑํ•œ ๋ชจ๋“  ์š”์†Œ ์ถœ๋ ฅ

class Student {
  constructor (name, age, enrolled, score) {
    this.name = name;
    this.age = age;
    this.enrolled = enrolled;
    this.score = score;
  }
}


const students = [
  new Student ('A', 29, true, 45),
  new Student ('B', 28, false, 80),
  new Student ('C', 30, true, 90),
  new Student ('D', 40, false, 66),
  new Student ('E', 18, true, 88),
];


// ๋“ฑ๋ก๋œ ํ•™์ƒ(enrolled === true) ๋ชจ๋‘ ์ฐพ๊ธฐ
const result = students.filter( student => student.enrolled);
console.log(result)

// ์ถœ๋ ฅ๊ฒฐ๊ณผ: Student {
//            name: 'A',
//            age: 29,
//            enrolled: true,
//            score: 45,
//            __proto__: Student { constructor: ฦ’ Student() }
//          }
//          Student {
//            name: 'C',
//            age: 30,
//            enrolled: true,
//            score: 90,
//            __proto__: Student { constructor: ฦ’ Student() }
//          }
//          Student {
//            name: 'E',
//            age: 18,
//            enrolled: true,
//            score: 88,
//            __proto__: Student { constructor: ฦ’ Student() }
//          }

๐Ÿน Some

๐Ÿ‘‰ ๋ฐฐ์—ด ์•ˆ์˜ ์–ด๋–ค ์š”์†Œ๋ผ๋„ ์ฃผ์–ด์ง„ ํŒ๋ณ„ ํ•จ์ˆ˜(์ฝœ๋ฐฑํ•จ์ˆ˜)๋ฅผ ํ†ต๊ณผํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•œ๋‹ค
์กฐ๊ฑด์— ํ•˜๋‚˜๋ผ๋„ ์ถฉ์กฑํ•˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.some ( el => ์กฐ๊ฑด)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: true/false ๋ฐ˜ํ™˜

// 50์  ์ดํ•˜์˜ ํ•™์ƒ์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ํ…Œ์ŠคํŠธ(์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false)
const result = students.some( student => student.score < 50;
console.log(result)
// true

โœ… ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜์—ฌ returnํ•˜๋Š” ๋ฉ”์„œ๋“œ

๐Ÿถ map

๐Ÿ‘‰ ๋ฐฐ์—ด ๋‚ด ๋ชจ๋“  ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์„œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.map( el => ์กฐ๊ฑด)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: ์ƒˆ๋กœ์šด ๋ฐฐ์—ด return

// ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜๋งŒ ๋ฐ˜ํ™˜
const result = students.map( student => student.score)
console.log(result);
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: [45, 80, 90, 66, 88]


// ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜๋งŒ ๋ฐ˜ํ™˜ํ•œ ๋ฐฐ์—ด์— 10์”ฉ ๋”ํ•ด์ฃผ๊ธฐ
const sumResult = result.map( el => el + 10)
console.log(result);
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: [55, 90, 100, 76, 98]

๐Ÿถ forEach

๐Ÿ‘‰ ๋ฐฐ์—ด ๋‚ด ๋ชจ๋“  ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์„œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.map( el => ์กฐ๊ฑด)
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: ์ƒˆ๋กœ์šด ๋ฐฐ์—ด return

const result = students.map( student => student.score)
console.log(result);
// ์ถœ๋ ฅ๊ฒฐ๊ณผ: [45, 80, 90, 66, 88]

๐Ÿ  reduce

๐Ÿ‘‰ ๋ฐฐ์—ด ๋‚ด ๋ชจ๋“  ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•˜์—ฌ ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ด์„œ ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜
๐Ÿ‘‰ ๋ฌธ๋ฒ•: array.reduce( (๋ˆ„์ ๊ฐ’, ํ˜„์žฌ๊ฐ’, index, el) => {return ๊ฒฐ๊ณผ}, ์ดˆ๊ธฐ๊ฐ’ );
๐Ÿ‘‰ ๋ฐ˜ํ™˜๊ฐ’: ์ƒˆ๋กœ์šด ๋ฐฐ์—ด return

// ๊ณต์‹
let sum = arr.reduce( (acc, cur, index, el) => { return acc + cur }, 0);
console.log(sum)



// ์˜ˆ์‹œ
const numArr = [1, 2, 3, 4, 5]

let result = numArr.reduce( (acc, cur, index) => {
// console.log(acc, cur, index)
// (๋ˆ„์  ํ˜„์žฌ๊ฐ’ index)
// 0 1 0
// 1 2 1
// 3 3 2
// 6 4 3
// 10 5 4
	return acc + cur;
} , 0);

console.log(result)   // 15 (์ด ๋ˆ„์ ๊ฐ’ ์ถœ๋ ฅ)



// ๋ˆ„์ ๊ฐ’๊ณผ ํ˜„์žฌ๊ฐ’๋งŒ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ˆ์‹œ
const numArr = [1, 2, 3, 4, 5]

let result = numArr.reduce( (acc, cur ) => {
// console.log(acc, cur)
// 0 1
// 1 2
// 3 3
// 6 4
// 10 5
	return acc + cur;
}, 0);

console.log(result)
profile
boma91@gmail.com

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