๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
WEB/JavaScript

[JavaScript] push() & concat()

by mingzoo 2021. 1. 20.

์˜ค๋Š˜์€ ๋ฆฌ์•กํŠธ๋ฅผ ๊ณต๋ถ€ํ•˜๋‹ค๊ฐ€ ๋ฏธ๋ค„๋†“์•˜๋˜ ๊ถ๊ธˆ์ฆ์„ ํ’€์–ด๋ณด์•˜๋‹ค.

๐Ÿ“–๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ๐Ÿ“–์˜ 6์žฅ์—์„œ ๋‚ด์žฅํ•จ์ˆ˜์— ๋Œ€ํ•œ ์–ธ๊ธ‰์ด ์žˆ์—ˆ๋Š”๋ฐ, ๊ฑฐ๊ธฐ์— concat, filter์™€ ๊ฐ™์€ ํ•จ์ˆ˜๊ฐ€ ์žˆ์—ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ concat ํ•จ์ˆ˜๋ฅผ ์ฐพ์•„๋ณด๋‹ค๋ณด๋‹ˆ pushํ•จ์ˆ˜์™€ ๋น„๊ต๊ฐ€ ๋งŽ์ด ๋˜๊ธธ๋ž˜ ๊ฐ™์ด ๊ณต๋ถ€ํ•ด๋ณด์•˜๋‹ค.

 


์ด ๋‘ ํ•จ์ˆ˜๋“ค์€ ๋ฐฐ์—ด์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ๊ทธ๋Ÿผ ๊ทธ ์‚ฌ์ด์—์„œ ๋‘ ํ•จ์ˆ˜์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ?

๋จผ์ €, push ํ•จ์ˆ˜๋ฅผ ์•Œ์•„๋ณด๊ณ , concatํ•จ์ˆ˜์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ๋‹ค.

์™œ ๋‘ ํ•จ์ˆ˜๋ฅผ ๊ฐ™์ด ๋น„๊ตํ•˜๋Š”๊ฒƒ์ธ๊ฐ€?

๊ทธ ์ด์œ ๋Š” ๋‘ ๊ฐœ์˜ ๋ฐฐ์—ด์„ ํ•˜๋‚˜์˜ ๋ฐฐ์—ด๋กœ ํ•ฉ์น  ๋•Œ, ์ผ์–ด๋‚˜๋Š”๋ฐ ํ•˜๋‚˜์˜ ์›์†Œ๋ฅผ ๋ฐ€์–ด๋„ฃ๋Š” push ๋Œ€์‹ ์— concat์„ ์‚ฌ์šฉํ•˜๋ผ๊ณ  ๋‘ ํ•จ์ˆ˜๋ฅผ ๊ฐ™์ด ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

์ด ๊ธ€์€ MDN์„ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€๋‹ค.

 

push()

push๋Š” ๊ธฐ์กด ๋ฐฐ์—ด์— ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉฐ ๋ฐฐ์—ด์˜ ์ด ๊ธธ์ด๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. 

์ด ๋ฉ”์†Œ๋“œ๋Š” arr.push([element1[, ...[, elementN]]]) ์ด ํ˜•์‹์ฒ˜๋Ÿผ ์ž‘์„ฑํ•ด์ฃผ๋ฉด๋œ๋‹ค.

push๋ฉ”์†Œ๋“œ๋Š” ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ์ž‘๋™๋œ๋‹ค.

 

const animals = ['pigs', 'goats', 'sheep'];

const count = animals.push('cows');
console.log(count);
// expected output: 4
//๋ณ€์ˆ˜ count๋Š” push๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด์„œ cows์›์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ ๋ฐฐ์—ด animals์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push('chickens', 'cats', 'dogs');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

 

push()๋ฉ”์†Œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์„ธ๊ฐ€์ง€ ๊ฒฝ์šฐ์— ์‚ฌ์šฉ์ด ๋œ๋‹ค.

  1. ๋ฐฐ์—ด์— ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ
  2. ๋‘๊ฐœ์˜ ๋ฐฐ์—ด์„ ๋ณ‘ํ•ฉํ•  ๋•Œ
  3. ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ƒ๊ธด ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ( Using an object in an array-like fashion) - ์œ ์‚ฌ๋ฐฐ์—ด

 

1,2๋ฒˆ์˜ ์˜ˆ์‹œ์ฝ”๋“œ๋Š” ์œ„์— ๋‚˜ํƒ€๋‚˜์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด, ์„ธ๋ฒˆ์งธ ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ?

Array.prototype.push๊ฐ€ ๊ฐ์ฒด์—์„œ๋„ ์‚ฌ์šฉ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์˜ˆ์‹œ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด์„œ ์ดํ•ดํ•ด๋ณด์ž

 

let obj = {
    length: 0,

    addElem: function addElem(elem) {
        // obj.length is automatically incremented
        // every time an element is added.
        [].push.call(this, elem)
    }
}

// Let's add some empty objects just to illustrate.
obj.addElem({})
obj.addElem({})
console.log(obj.length)
// → 2

 

obj๊ฐ€ ๋ฐฐ์—ด์ด ์•„๋‹˜์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ , push()๋ฉ”์†Œ๋“œ๊ฐ€ ์ ์šฉ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ์ฒด์— ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

ํ•˜์ง€๋งŒ, ๊ฐ์ฒด์—์„œ call๊ณผ Array.prototype.push๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฐฐ์—ด๋กœ ์ฐฉ๊ฐํ•˜๊ณ  ์‹คํ–‰๋˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

(์œ ์‚ฌ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ์—” ์›๋ž˜ ๋ฐฐ์—ด์˜ ๋ฉ”์„œ๋“œ์˜ ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋ฐฐ์—ด ํ”„๋กœํ† ํƒ€์ž…์—์„œ forEach ๋ฉ”์„œ๋“œ๋ฅผ ๋นŒ๋ ค์˜ค๋ฉด ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค. -> call, apply)

์œ ์‚ฌ๋ฐฐ์—ด๊ณผ ๋ฐฐ์—ด์„ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Array.isArray๋ฉ”์„œ๋“œ๋กœ ํ™•์ธํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋‚˜๋Š” ์ด ๋ถ€๋ถ„์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์œ ์‚ฌ๋ฐฐ์—ด์ด๋ผ๋Š” ๊ฒƒ์„ ์ฒ˜์Œ ์•Œ๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ์œ ์‚ฌ๋ฐฐ์—ด๊ณผ ๋ฐฐ์—ด์˜ ์ฐจ์ด์ ์€ ์—ฌ๊ธฐ์„œ ๋” ์ž์„ธํ•˜๊ฒŒ ๋ณด๊ณ  ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•˜๊ฒŒ ๊ธฐ์–ตํ•ด์•ผ๋˜๋Š” ๊ฒƒ์€ ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’์ด๋‹ค.

๋“ค์–ด๊ฐ€๋Š” ๋ณ€์ˆ˜ : ํ•ด๋‹น ๋ฐฐ์—ด์˜ ๋์ž๋ฆฌ์— ๋„ฃ๊ณ ์‹ถ์€ element

๋ฆฌํ„ด๊ฐ’ : push()๋ฉ”์†Œ๋“œ๊ฐ€ ๋ถˆ๋ฆฐ ์ดํ›„์˜ ์ƒˆ๋กœ์šด ๊ธธ์ด๊ฐ’์„ ๋ฆฌํ„ด

 


concat()

concat()๋ฉ”์†Œ๋“œ๋Š” ๋‘ ๊ฐœ ์ด์ƒ์˜ ๋ฐฐ์—ด์„ ๋ณ‘ํ•ฉํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.

์ด ๋ฉ”์†Œ๋“œ๋Š” ๊ธฐ์กด์˜ ๋ฐฐ์—ด์„ ๋ฐ”๊พธ์ง€ ์•Š๊ณ , ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

๊ธฐ์กด ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•œ ํ›„ ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ƒˆ ๋ฐฐ์—ด์„ ๋ฆฌํ„ด

concat()๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‘ ๋ฐฐ์—ด์„ ํ•ฉ์น˜๋Š” ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

const letters = ['a', 'b', 'c'];
const numbers = [1, 2, 3];

letters.concat(numbers);
// result in ['a', 'b', 'c', 1, 2, 3]

 

์—ฌ๊ธฐ์„œ push()์™€์˜ ์ฐจ์ด๋Š” ๋ฌด์—‡์ผ๊นŒ?

๋ฐ”๋กœ ๋ฌธ์ž์—ด๋กœ ์ถ”๊ฐ€๋˜๋Š”์ง€, ๋ฐฐ์—ด ์ž์ฒด๋กœ ์ถ”๊ฐ€ ๋˜๋Š”์ง€๊ฐ€ ์ฐจ์ด์ ์ด๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ํ•œ ๋ฒˆ ํ™•์ธํ•ด๋ณด์ž

 

const letters = ['a', 'b', 'c'];
const numbers = [1, 2, 3];

//concat์„ ์‚ฌ์šฉํ•  ๋•Œ
letters.concat(numbers);
// result in ['a', 'b', 'c', 1, 2, 3]

//push๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ
letters.push(numbers);
console.log(letters);
// result in ['a', 'b', 'c', [1, 2, 3]]

 

์œ„์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ๋ฌด์Šจ ์ฐจ์ด๊ฐ€ ์žˆ๋Š”์ง€ ํ•œ ๋ˆˆ์— ํ™•์ธ์ด ๋˜์ฃ ?!?!?

 

์ž, ๊ทธ๋Ÿฌ๋ฉด ๋‹ค์Œ์œผ๋กœ๋Š” ๋ฐฐ์—ด์•ˆ์— ์žˆ๋Š” ๋ฐฐ์—ด์ด ์žˆ์„ ๋•Œ์ธ ์ค‘์ฒฉ๊ตฌ์กฐ์—๋Š” push๋‚˜ concat์„ ํ–ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ๋ฐ˜์˜์ด ๋ ์ง€ ๋ณด๊ฒ ๋‹ค.

๋ฐ‘์˜ ์˜ˆ์‹œ๋ฅผ ๋จผ์ € ๋ด๋ณด์ž.

 

const num1 = [[1]];
const num2 = [2, [3]];

const numbers = num1.concat(num2);

console.log(numbers);
// results in [[1], 2, [3]]

// modify the first element of num1
num1[0].push(4);

console.log(numbers);
// results in [[1, 4], 2, [3]]

 

num1[0].push(4); ์ด ์ฝ”๋“œ๋Š” num1๋ฐฐ์—ด์˜ 0๋ฒˆ์งธ ์š”์†Œ์— 4๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋ผ๋Š” ๋ช…๋ น์ด๋‹ค.

๋”ฐ๋ผ์„œ, ๊ฒฐ๊ณผ๋กœ ์›์†Œ๊ฐ€ 1๋งŒ ์žˆ์—ˆ๋˜ ๋ฐฐ์—ด์•ˆ์— [1,4]๋กœ ์ถœ๋ ฅ์ด ๋˜๋Š”๊ฒƒ์„ ํ™•์ธํ•ด์ค„์ˆ˜ ์žˆ๋‹ค.

 


์ด๋ ‡๊ฒŒ ๋‘๊ฐ€์ง€ ๋ฉ”์†Œ๋“œ๋“ค์„ ์ •๋ฆฌํ•˜๋‹ค๋ณด๋‹ˆ, push()๋Š” ํ•˜๋‚˜์˜ ์š”์†Œ๋‚˜ ์•„์ดํ…œ๋“ค์„ ํ•˜๋‚˜์”ฉ ํ•˜๋‚˜์”ฉ ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•  ๋•Œ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ณ , ๋ฐฐ์—ด์ž์ฒด๋ฅผ ๋‹ค๋ฃฐ ๋•Œ์—๋Š” concat()์„ ์“ฐ๋Š”๊ฒŒ ๋” ํšจ์œจ์ ์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , push()๊ฐ€ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ๊ฐ’์ด ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ผ๋Š” ์ ์„ ๊ธฐ์–ตํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

๋‹ค๋ฅธ ๋‚ด์žฅํ•จ์ˆ˜๋“ค๋„ ๋งŽ์ด ๊ณต๋ถ€ํ•˜๊ณ  ์จ๋ด์„œ ๋” ํšจ์œจ์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ๊ฒ ๋‹ค!๐Ÿคจ๐Ÿ˜

728x90