๋ฐ˜์‘ํ˜•

Mutable / Immutable


Array.sort(), Array.reverse(), Array.splice() ๊ฐ™์€ ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋Š” ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•œ๋‹ค. ๊ฐ์ฒด(๋ฐฐ์—ด) ์ž์ฒด๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•œ ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์—์„œ reversed๋Š” ๊ฒฐ๊ณผ์ ์œผ๋กœ regions ๋ฐฐ์—ด์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค. ์ฆ‰, regions reversed ์ด ๋‘˜์€ ๋™์ผํ•˜๋‹ค.

const regions = ['Seoul', 'Shanghai', 'Tokyo'];
const reversed = regions.reverse();
console.log(regions); // ['Tokyo', 'Shanghai', 'Seoul']
console.log(reversed); // ['Tokyo', 'Shanghai', 'Seoul']

// ๋‘ ๊ฐ’์ด ๋™์ผํ•œ์ง€ ์—ฌ๋ถ€, ๋น„๊ตํ•˜๋Š” ๊ฐ’์ด ๊ฐ์ฒด์ผ ๋• ์ฐธ์กฐ๊ฐ’ ๋น„๊ต
Object.is(regions, reversed); // true

 

๐Ÿ’ก Immer ๊ฐ™์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

ํ•œํŽธ, ๋ฆฌ์•กํŠธ์—์„œ ๋ถˆ๋ณ€์„ฑ์„ ์ง€ํ‚ค์ง€ ์•Š๊ณ  ๊ฐ์ฒด๋ฅผ ๋ณ€๊ฒฝํ•œ ํ›„ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•˜๋ฉด ๋ฆฌ๋ Œ๋”๋ง์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋Ÿฌํ•œ ์ด์œ ๋กœ ๋ฆฌ์•กํŠธ ๊ณต์‹ ๋ฌธ์„œ์—์„  push, splice, reverse ๊ฐ™์€ mutable ๋ฉ”์„œ๋“œ ๋Œ€์‹ , concat, ์ „๊ฐœ์—ฐ์‚ฐ์ž, filter, slice, map ๊ฐ™์€ immutable ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค.

 

Array.from(), ์ „๊ฐœ ์—ฐ์‚ฐ์ž, slice() ๋“ฑ์„ ํ†ตํ•ด ๋ฐฐ์—ด ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค๊ณ  ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋ถˆ๋ณ€์„ฑ์„ ์ง€ํ‚ฌ ์ˆ˜ ์žˆ์ง€๋งŒ, ๋งค๋ฒˆ ๋ฐฐ์—ด ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“œ๋Š”๊ฑด ๋ฒˆ๊ฑฐ๋กœ์šด ์ผ์ด๋‹ค.

 

ES2023์— ์ƒˆ๋กœ ๊ณต๊ฐœํ•œ toSorted, toReversed, toSpliced, with ๋ฉ”์„œ๋“œ๋Š” ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜๊ณ , ๋ณต์‚ฌ๋ณธ์— ๋Œ€ํ•ด ๋ณ€๊ฒฝ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋”ฐ๋กœ ๋ฐฐ์—ด ๋ณต์‚ฌ๋ณธ์„ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋ผ์„œ ๊ฐ€๋…์„ฑ๋„ ์ข‹์•„์ง„๋‹ค.

 

ES2023 ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ


์•„๋ž˜ ES2023 ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” TypedArray์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
(TypedArray์—” splice ๋ฉ”์„œ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ toSpliced ๋ฉ”์„œ๋“œ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€)

 

Array.toSorted

Array.toSorted() ๋ฉ”์„œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ Array.sort() ๋ฉ”์„œ๋“œ์™€ ๋™์ผํ•˜์ง€๋งŒ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค. ์›๋ณธ ๋ฐฐ์—ด์—๋Š” ์•„๋ฌด๋Ÿฐ ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ด๋“œ ์ดํŽ™ํŠธ ์—†์ด ์ˆœ์ˆ˜ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋™์ž‘ํ•œ๋‹ค.

 

Array.sort() ์‚ฌ์šฉ ์‹œ โ–ผ

const numbers = [1, 2, 3, 4, 5];
const descNumbers = numbers.sort((a, b) => b - a); // ์›๋ณธ ๋ฐฐ์—ด ๋ณ€๊ฒฝ

Object.is(numbers, descNumbers); // true

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

 

Array.toSorted() ์‚ฌ์šฉ ์‹œ โ–ผ

const numbers = [1, 2, 3, 4, 5];
const descNumbers = numbers.toSorted((a, b) => b - a); // ๋ณต์‚ฌ๋ณธ ์ƒ์„ฑ

Object.is(numbers, descNumbers); // false

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

 

Array.toReversed

Array.toReversed() ๋ฉ”์„œ๋“œ๋Š” Array.reverse() ๋ฉ”์„œ๋“œ์™€ ์œ ์‚ฌํ•˜๊ฒŒ ๋™์ž‘ํ•˜์ง€๋งŒ, ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ์ˆœ์„œ๊ฐ€ ๋ฐ˜์ „๋œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

Array.reverse() ์‚ฌ์šฉ ์‹œ โ–ผ

const numbers = [1, 2, 3, 4, 5];
const descNumbers = numbers.reverse(); // ์›๋ณธ ๋ฐฐ์—ด ๋ณ€๊ฒฝ

Object.is(numbers, descNumbers); // true

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

 

Array.toReversed() ์‚ฌ์šฉ ์‹œ โ–ผ

const numbers = [1, 2, 3, 4, 5];
const descNumbers = numbers.toReversed(); // ๋ณต์‚ฌ๋ณธ ์ƒ์„ฑ

Object.is(numbers, descNumbers); // false

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

 

Array.toSpliced

Array.toSpliced(start, deleteCount, item1, item2, itemN)

 

Array.toSpliced()๋Š” Array.splice()์™€ ์‚ด์ง ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•œ๋‹ค. splice() ๋ฉ”์„œ๋“œ๋Š” ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜๊ณ , ์‚ญ์ œํ•œ ์š”์†Œ๋ฅผ ํฌํ•จํ•œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜์ง€๋งŒ, toSpliced() ๋ฉ”์„œ๋“œ๋Š” ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ , ์‚ญ์ œํ•˜์ง€ ์•Š์€ ์š”์†Œ๋ฅผ ํฌํ•จํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const numbers = [1, 2, 3, 4, 5];
const spliced = numbers.splice(0, 1);

console.log(numbers); // [2, 3, 4, 5] -> ์›๋ณธ ๋ฐฐ์—ด ๋ณ€๊ฒฝ
console.log(spliced); // [1] -> ์‚ญ์ œํ•œ ์š”์†Œ
const numbers = [1, 2, 3, 4, 5];
const spliced = numbers.toSpliced(0, 1);

console.log(numbers); // [1, 2, 3, 4, 5] -> ์›๋ณธ ๋ฐฐ์—ด ์œ ์ง€
console.log(spliced); // [2, 3, 4, 5] -> ์‚ญ์ œํ•˜์ง€ ์•Š์€ ์š”์†Œ

 

๋งŒ์•ฝ ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ , ์‚ญ์ œํ›„ ๋‚จ์€ ์š”์†Œ์™€ ์‚ญ์ œํ•œ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ slice() ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

const numbers = [1, 2, 3, 4, 5];
const start = 0, count = 1

const spliced = numbers.toSpliced(start, count) // [2, 3, 4 ,5] -> ์‚ญ์ œ ํ›„ ๋‚จ์€ ์š”์†Œ
const removed = numbers.slice(start, start + count); // [1] -> ์‚ญ์ œํ•œ ์š”์†Œ

 

Array.with

Array.with(index, value)

 

๋ฐฐ์—ด์—์„œ ํŠน์ • ์ธ๋ฑ์Šค์— ์žˆ๋Š” ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ๋ธŒ๋ผ์ผ“ ๋…ธํ…Œ์ด์…˜(Bracket Notation)์„ ์‚ฌ์šฉํ•œ๋‹ค. ์›๋ณธ ๋ฐฐ์—ด์„ ์ง์ ‘ ๋ณ€๊ฒฝํ•˜๋Š” mutable ๋ฐฉ์‹์ด๋‹ค.

const numbers = [1, 2, 3, 4, 5];
numbers[0] = 100;

console.log(numbers); // [100, 2, 3, 4, 5]

 

Array.with ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์›๋ณธ ๋ฐฐ์—ด์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ํŠน์ • ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

const numbers = [1, 2, 3, 4, 5];
const updated = numbers.with(0, 100);

console.log(numbers); // [1, 2, 3, 4, 5];
console.log(updated); // [100, 2, 3, 4, 5]

 

Array ์ƒ์†


extends ํ‚ค์›Œ๋“œ๋กœ ๋‚ด์žฅ Array ๊ฐ์ฒด๋ฅผ ์ƒ์†๋ฐ›์€ ์ธ์Šคํ„ด์Šค์—์„œ map, flatMap, filter, concat ๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’์€ ๊ธฐ์กด ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋œ๋‹ค. ์ฆ‰, ๋™์ผํ•œ ํƒ€์ž…์˜ ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋ฐ˜๋ฉด toSorted, toReversed, toSpliced, with ๋ฉ”์„œ๋“œ๋Š” Array ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

class MyArray extends Array {}
const numbers = new MyArray(1, 2, 3, 4, 5);

const multiplied = numbers.map(n => n * 2);
console.log(multiplified instanceof MyArray); // true -> MyArray ์ธ์Šคํ„ด์Šค

const reversed = numbers.toReversed();
console.log(reversed instanceof MyArray); // false -> Array ์ธ์Šคํ„ด์Šค

 

MyArray.from์„ ์‚ฌ์šฉํ•˜๋ฉด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋„ MyArray ์ธ์Šคํ„ด์Šค๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

class MyArray extends Array {}
const numbers = new MyArray(1, 2, 3, 4, 5);

const reversed = MyArray.from(numbers.toReversed());
console.log(reversed instanceof MyArray); // true

 

๋ธŒ๋ผ์šฐ์ € ์ง€์›


  • Chrome 110 (Released 2023-02-07)
  • Safari 16.3
  • Node.js 20
  • Deno 1.31

 

๋ ˆํผ๋Ÿฐ์Šค


 

ES2023 introduces new array copying methods to JavaScript

ES2023 is introducing new array methods in JavaScript & they are here to make our programs more predictable and maintainable by copying instead of mutating.

www.sonarsource.com

 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•