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

๐Ÿ“ฐ ์–ธ์–ด/JS

๋ฐฐ์—ด๊ณผ ๋ฐฐ์—ด์ฒ˜๋ฆฌ

์ฑ… <๋Ÿฌ๋‹์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ>๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ๋ฐฐ์šด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

๋ชฉ์ฐจ

  • 1. ๋ฐฐ์—ด์˜ ์š”์†Œ ์กฐ์ž‘
  • 2. ๋ฐฐ์—ด ๊ฒ€์ƒ‰
  • 3. ๋ฐฐ์—ด์˜ ์š”์†Œ ๋ณ€ํ˜•
  • 4. ๋ฐฐ์—ด ์ž์ฒด๋ฅผ ๋ณ€ํ˜•
  • 5. ๋ฌธ์ž์—ด ๋ณ‘ํ•ฉ 

 

<๋ฐฐ์—ด๊ณผ ๋ฐฐ์—ด์ฒ˜๋ฆฌ>

๋ฐฐ์—ด์˜ ์š”์†Œ ์กฐ์ž‘ ๋ฐฐ์—ด์— ์š”์†Œ ํ•˜๋‚˜ ์ถ”๊ฐ€/์ œ๊ฑฐ push/pop(๋) unshift/shif(์ฒ˜์Œ)
๋ฐฐ์—ด์˜ ๋์— ์—ฌ๋Ÿฌ ์š”์†Œ ์ถ”๊ฐ€ concat
๋ฐฐ์—ด์ผ๋ถ€ ๊ฐ€์ ธ์˜ค๊ธฐ slice
์ž„์˜์˜ ์œ„์น˜์— ์š”์†Œ ์ถ”๊ฐ€/์ œ๊ฑฐ splice
๋ฐฐ์—ด ์•ˆ์—์„œ ์š”์†Œ ๊ต์ฒดํ•˜๊ธฐ copyWithin
ํŠน์ • ๊ฐ’์œผ๋กœ ๋ฐฐ์—ด ์ฑ„์šฐ๊ธฐ fill
๋ฐฐ์—ด ์ •๋ ฌ sort/reverse
๋ฐฐ์—ด ๊ฒ€์ƒ‰ ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๊ฒ€์ƒ‰ indexOf/lastIndexOf   ๋˜๋Š” findIndex
์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ ๊ฒ€์ƒ‰ find
some๊ณผ every
๋ฐฐ์—ด์˜ ์š”์†Œ ๋ณ€ํ˜• ์š”์†Œ ๋ณ€ํ˜•, ์‚ฌ๋ณธ ๋ฐ˜ํ™˜ map
์š”์†Œ ๋ณ€ํ˜•, ๋ฐฐ์—ด์—์„œ ํ•„์š”ํ•œ ๊ฒƒ๋งŒ ๋‚จ๊ธด ์‚ฌ๋ณธ ๋ฐ˜ํ™˜ filter
๋ฐฐ์—ด ์ž์ฒด๋ฅผ ๋ณ€ํ˜• reduce
๋ฌธ์ž์—ด ๋ณ‘ํ•ฉ Array.prototype.join(๊ตฌ๋ถ„์ž)

 

1. ๋ฐฐ์—ด์˜ ์š”์†Œ ์กฐ์ž‘

1-1) ๋ฐฐ์—ด์— ์š”์†Œ ํ•˜๋‚˜ ์ถ”๊ฐ€/์ œ๊ฑฐ

  • push/pop - ๋ฐฐ์—ด์˜ ๋์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€/์ œ๊ฑฐ
  • unshift/shif - ๋ฐฐ์—ด์˜ ์ฒ˜์Œ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€/์ œ๊ฑฐ
var fruits = ["Apple","Banana","Orange","Strawberry"];

var push = fruits.push("Grape");  //๋งจ ๋์— "Grape"์ถ”๊ฐ€
var pop = fruits.pop();           //๋งจ ๋์— "Grape"์ œ๊ฑฐ
var unshift = fruits.unshift("Grape");  //๋งจ ์•ž์— "Grape"์ถ”๊ฐ€
var shift = fruits.shift();             //๋งจ ์•ž์— "Grape"์ œ๊ฑฐ

console.log(push);
console.log(pop);
console.log(unshift);
console.log(shift);

console.log(fruits);

1-2) ๋ฐฐ์—ด์˜ ๋์— ์—ฌ๋Ÿฌ ์š”์†Œ ์ถ”๊ฐ€

  • concat - ๋ฐฐ์—ด์˜ ๋์— value ์š”์†Œ๋“ค์„ ์ถ”๊ฐ€ํ•œ ์ƒˆ๋กœ์šด ์‚ฌ๋ณธ์„ ๋ฐ˜ํ™˜
arr.concat(value);
var arr = [1, 2, 3];

var newArr = arr.concat(4, 5, 6);      // newArr: [1, 2, 3, 4, 5, 6]
var newArr = arr.concat([4, [5, 6]]);  // newArr: [1, 2, 3, 4, [5, 6]]

1-3) ๋ฐฐ์—ด์ผ๋ถ€ ๊ฐ€์ ธ์˜ค๊ธฐ

  • slice( a, b ) - ์ธ๋ฑ์Šค a~(b-1)๊นŒ์ง€ ๊ฐ€์ ธ์˜ด
var fruits = ["Apple","Banana","Orange","Strawberry"];
var a = [1, 2, 3, 4, 5];
a.slice(0, 3); // [1, 2, 3]์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
a.slice(3); // [4, 5]๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 
a.slice(1, -1); // [2, 3, 4]๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 
a.slice(-3, -2) // [3]์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

1-4) ์ž„์˜์˜ ์œ„์น˜์— ์š”์†Œ ์ถ”๊ฐ€/์ œ๊ฑฐ

  • splice( x, n, ์ถ”๊ฐ€๋  ์š”์†Œ๋“ค ) - ์ธ๋ฑ์Šค x์—์„œ n๊ฐœ๋งŒํผ ๊ฐ€์ ธ์˜ค๊ธฐ
var fruits = ["Apple","Banana","Orange","Strawberry"];
var splice = fruits.splice(1, 2);  //fruits[1]๋ถ€ํ„ฐ 2๊ฐœ ๊ฐ€์ ธ์˜ค๊ธฐ
console.log(splice);
console.log(fruits);

#์ถœ๋ ฅ๊ฒฐ๊ณผ
["Banana","Orange"]
["Apple","Strawberry"]

1-5) ๋ฐฐ์—ด ์•ˆ์—์„œ ์š”์†Œ ๊ต์ฒดํ•˜๊ธฐ

  • copyWithin( x, a, b ) - ์ธ๋ฑ์Šค a~(b-1)๊นŒ์ง€ ๋ณต์‚ฌํ•ด ์ธ๋ฑ์Šค x์— ๋ณต์‚ฌํ•œ ์š”์†Œ๋ฅผ ๋ถ™์—ฌ๋†“์Œ 
const arr = [1, 2, 3, 4];

arr.copyWithin(1, 2)    // arr = [1, 3, 4, 4]
arr.copyWithin(2, 0, 2)    // arr = [1, 3, 1, 3]
arr.copyWithin(0, -3, -1)    // arr = [3, 1, 1, 3]

1-6) ํŠน์ • ๊ฐ’์œผ๋กœ ๋ฐฐ์—ด ์ฑ„์šฐ๊ธฐ

  • fill( x, a, b ) - ์ธ๋ฑ์Šค a~(b-1)๊นŒ์ง€ x๋กœ ์ฑ„์›€
const arr = new Array(5).fill(1)    // arr = [1, 1, 1, 1, 1]๋กœ ์ดˆ๊ธฐํ™”๋จ

arr.fill("a");    // arr = ["a", "a", "a", "a", "a"]
arr.fill("b", 1)    // arr = ["a", "b", "b", "b", "b"]
arr.fill("c", 2, 4) // arr = ["a", "b", "c", "c", "b"]
arr.fill(5.5, -4)     // arr = ["a", 5.5, 5.5, 5.5, 5.5]

1-7) ๋ฐฐ์—ด ์ •๋ ฌ

  • sort() - ์ •๋ ฌ
  • reverse() - ์—ญ์ˆœ ์ •๋ ฌ

 

 

2. ๋ฐฐ์—ด ๊ฒ€์ƒ‰

2-1) ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๊ฒ€์ƒ‰

  • indexOf/ lastIndexOf - ๋ฐฐ์—ด ์ฒ˜์Œ/ ๋์—์„œ ๋ถ€ํ„ฐ ์š”์†Œ ๊ฒ€์ƒ‰ํ•ด ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
  • findIndex - ๋ณด์กฐ ํ•จ์ˆ˜๋กœ ๊ฒ€์ƒ‰ ์กฐ๊ฑด ์ง€์ •ํ•˜์—ฌ ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅ, ์—†์„ ๊ฒฝ์šฐ -1๋ฐ˜ํ™˜

2-2) ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ ๊ฒ€์ƒ‰

  • find - ๋ณด์กฐ ํ•จ์ˆ˜๋กœ ๊ฒ€์ƒ‰ ์กฐ๊ฑด ์ง€์ •ํ•˜์—ฌ ๊ฒ€์ƒ‰ ๊ฐ€๋Šฅ, ์—†์„ ๊ฒฝ์šฐ undefinded๋ฐ˜ํ™˜
#indexOf/ lastIndexOf ์˜ˆ์ œ
let str = 'Brave new world';

console.log(str.indexOf('w'));  // output>  8
console.log(str.lastIndexOf('w'));  // output>  10
console.log(str.indexOf('new'));  // output>  6
console.log(str.lastIndexOf('new'));  // output>  6
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
//findIndex
const findIndexValue = arr.findIndex(data => data % 2);
console.log('findIndexValue', findIndexValue); // 0

//find
const findValue = arr.find(data => data % 2);
console.log('findValue', findValue); // 1

โ€ปfind์™€ findIndex์— ์ „๋‹ฌํ•˜๋Š” ํ•จ์ˆ˜

  • ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ๊ฒƒ : 1. ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ  2. ํ˜„์žฌ ์š”์†Œ์˜ ์ธ๋ฑ์Šค, ๋ฐฐ์—ด ์ž์ฒด
  • this์ˆ˜์ • ๊ฐ€๋Šฅ : this์ด์šฉํ•˜๋ฉด ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ์ธ ๊ฒƒ์ฒ˜๋Ÿผ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
arr.find(function(p){
    return p.id === this.id
}, juliet);

2-3) some๊ณผ every

  • some - ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ ์ฐพ์œผ๋ฉด ์ฆ‰์‹œ ๊ฒ€์ƒ‰ ์ค‘๋‹จํ•˜๊ณ  true ๋ฐ˜ํ™˜, ์ฐพ์ง€ ๋ชปํ•˜๋ฉด false ๋ฐ˜ํ™˜
  • every - ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์กฐ๊ฑด์— ๋งž์•„์•ผ true ๋ฐ˜ํ™˜, ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๋Š” ์š”์†Œ๋ฅผ ์ฐพ์•„์•ผ false ๋ฐ˜ํ™˜

every์™€์˜ ์ฐจ์ด์ ์€ every๋Š” ๋ชจ๋“ ๊ฐ’์ด true์—ฌ์•ผํ•˜๊ณ  some์€ ํ•˜๋‚˜๋งŒ true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค.

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
//some
const someArr = arr.some(data => data > 2);
console.log('someArr', someArr); // true

//every
const everyValue = arr.every(data => data > 2);
console.log('everyValue', everyValue); // false

 

 

3. ๋ฐฐ์—ด์˜ ์š”์†Œ ๋ณ€ํ˜• - map๊ณผ filter

3-1) map - ๋ฐฐ์—ด ์š”์†Œ ๋ณ€ํ˜•, ์‚ฌ๋ณธ ๋ฐ˜ํ™˜

const cart = [{name: "Widget", price: 9.95}, {name: "Gadget", price: 22.95}];

const names = cart.map(x => x.name);  // ["Widget", "Gadget"]
const prices = cart.map(x => x.price); // [9.25, 22.95]
const discountPrices = price.map(x => x * 0.8); // [7.96, 18.36]

3-2) filter - ๋ฐฐ์—ด์—์„œ ํ•„์š”ํ•œ ๊ฒƒ๋งŒ ๋‚จ๊ธด ์‚ฌ๋ณธ ๋ฐ˜ํ™˜

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

words.filter(word => word.length > 6);  // ["exuberant", "destruction", "present"]

 

 

4. ๋ฐฐ์—ด ์ž์ฒด๋ฅผ ๋ณ€ํ˜• - reduce

4-1) reduce( a, x ) - ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ a์—๋Š” ๋ฐฐ์—ด์ด ์ค„์–ด๋“œ๋Š” ๋Œ€์ƒ์ธ accumulator, ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋ถ€ํ„ฐ๋Š” (ํ˜„์žฌ ๋ฐฐ์—ด์š”์†Œ, ํ˜„์žฌ ์ธ๋ฑ์Šค, ๋ฐฐ์—ด ์ž์ฒด) ์ด๋‹ค.

reduce๋Š” ์ด ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜, ์˜๋‹จ์–ด๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐฐ์—ด์„ ์ฒซ ๊ธ€์ž์— ๋”ฐ๋ผ ๋ฌถ๊ฑฐ๋‚˜, set์˜ ํ‰๊ท  ๋ฐ ๋ถ„์‚ฐ์„ ๊ณ„์‚ฐํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

#๋ฐฐ์—ด์˜ ์ด ํ•ฉ์„ ๋ฐ˜ํ™˜
const arr = [5, 7, 2, 4];
const sum = arr.reduce((a, x) => a += x, 0); // 18

 

 

5. ๋ฌธ์ž์—ด ๋ณ‘ํ•ฉ

5-1) Array.prototype.join(๊ตฌ๋ถ„์ž)

const arr = [1, null, "hello", "world", true, undefined];
delete arr[3];

arr.join();        // "1,,hello,,true,"
arr.join('');    // "1hellotrue"
arr.join(' -- ');    // "1 -- -- hello -- -- true -- "