ECMAScript 2023 ์ดํด๋ณด๊ธฐ ๐
์คํฐ๋์ ๋ถ์ ๋ธ๋ก๊ทธ๋ฅผ ๋ณด๋ ์ค ์๊ฒ ๋์ด ์๋ก ์ถ๊ฐ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋ฌธ๋ฒ๋ค์
๊ณต๋ถํ๋ฉฐ ๋ด ๋ธ๋ก๊ทธ์๋ ์ ๋ฆฌํ๊ณ ์ถ์ด ์ํด๋ฅผ ๊ตฌํ๊ณ ๐ ๋๋ ๋ถ์ํด๋ณด๊ฒ ๋์๋ค.
toSorted, toReversed, with, findLast, findLastIndex, toSpliced ...
1. to ~
โ toSorted
์๋ฐ์คํฌ๋ฆฝํธ๋ ๋ฉ์๋๋ง๋ค mutation ์ ๋ํ ์ผ๊ด์ฑ์ด ์๋ค.
์๋ฅผ ๋ค๋ฉด sort() ๊ณผ reverse() ๋ ์๋ณธ์ ๋ฐ๊ฟ๋ฒ๋ฆฌ๊ณ , filter ๋ ์๋ณธ์ ๋ณํ๊ฐ ์๋ค.
์๋ก ์๊ธด toSorted ๋ฉ์๋๋ ์๋ณธ์ ๋ณํ๊ฐ ์๋ค
์ด๋ ๊ฒ sort ๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ณธ ๋ฐฐ์ด์ด ๋ณํ๊ฐ ๋๋ค
์๋ก์๊ธด toSorted๋ฅผ ์ด์ฉํ๋ฉด, ์๋ณธ ๋ฐฐ์ด์ ๊ทธ๋๋ก ์ ์ง๊ฐ ๋๋ ๊ฒ์ ๋ณผ ์ ์์!
๊ทธ๋์ sort๋ฅผ ์ฌ์ฉํ ๋, ์๋ณธ ๋ฐฐ์ด์ด ๋ณํ์ง ์๊ฒ ํ๊ธฐ ์ํด์ ๋ณต์ฌ๋ฅผ ํ ๋ฒ ํด์ฃผ๋ ๊ณผ์ ์ ๊ฑฐ์ณ์ผํ๋๋ฐ
์ด ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ฉด ๊ทธ๋ฐ ๊ณผ์ ์ ๊ฑฐ์น์ง ์์๋ ๋๋น
โ toSpliced
toSpliced๋ ์๋ณธ์ ์ํฅ์ ๋ฏธ์น์ง ์๋๋ค. ์๋ณธ ๋ฐฐ์ด์ ๋ฐ๊พธ์ง ์๊ณ splice ์ดํ์ ๋ฐฐ์ด์ ๋ฐํํ๊ฒ ๋๋ ๊ฒ์ด๋ค.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
Array.prototype.splice() - JavaScript | MDN
splice() ๋ฉ์๋๋ ๋ฐฐ์ด์ ๊ธฐ์กด ์์๋ฅผ ์ญ์ ๋๋ ๊ต์ฒดํ๊ฑฐ๋ ์ ์์๋ฅผ ์ถ๊ฐํ์ฌ ๋ฐฐ์ด์ ๋ด์ฉ์ ๋ณ๊ฒฝํฉ๋๋ค.
developer.mozilla.org
๊ณต์ ๋ฌธ์์์ ๋ณผ ์ ์๋ฏ, splice() ๋ array.splice(start[, deleteCount[, item1[, item2[, ...]]]]) ๋ก splice(์ ๊ฑฐ๋ฅผ ์์ํ ์ธ๋ฑ์ค, ์ง์ธ ๊ฐ์, ์ถ๊ฐํ ์์) ์ด๋ ๊ฒ ์ด์ฉํ๋ ๊ฒ์ธ๋ฐ, toSpliced๋ ๋๊ฐ๋ค.
๋์ ์์ ๋งํ๋ฏ ์๋ณธ ๋ฐฐ์ด์ ๋ฐ๊พธ์ง ์๊ณ ์๋กญ๊ฒ ์์ฑํด์ ๋ฐํ ํด์ค๋ค.
splice() ํจ์๋ฅผ ๋จผ์ ๋ณด๋ฉด, ์๋ผ๋ธ ๋ถ๋ถ์ ๋ด์ ๋ธ๋ค.
๊ทธ๋ฌ๊ณ ์๋ณธ์ ์ถ๊ฐ๋ก ๋ณํ๋ ๋ฐฐ์ด๋ก ๋ฐ๋๋ค.
์ด์ฒ๋ผ ์๋ก์ด ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ฉด, ๋ณํ๋ ๋ฐฐ์ด์ ๋ฐํํด์ฃผ๊ณ ์๋ณธ ๋ฐฐ์ด์ ๋ณํ์ง ์๋ ๊ฒ์ ์ ์ ์๋ฐ!
๊ทธ๋ฌ๋ splice๋ฅผ ์ฌ์ฉํ๋ฉด, ์๋ผ๋ธ ๋ถ๋ถ์ ๋ถ๋ฆฌํ ์ ์๊ณ , toSpliced๋ฅผ ์ด์ฉํ๋ฉด ์๋ณธ๋ฐฐ์ด์ ์ด์ฉํ ์ ์๋ค๋ ๊ฒ์ ์ ์ ์๋น
โ toReversed
์์ ๋ณธ ๋ ๋ฉ์๋๋ก ์ ์ถํด๋ณด์, reverse์ ์ฐจ์ด๋ฅผ ๋ณด์ด๋ ๋ฉ์๋์ด๋ค.
reverse()๋ฅผ ์ด์ฉํ๋ฉด ์๋ณธ ๋ฐฐ์ด์ ๋ณํ์ํจ๋ค.
toReversed()๋ฅผ ์ด์ฉํ๋ฉด ์๋ณธ ๋ฐฐ์ด์ ๊ทธ๋๋ก ์ ์ง๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
์ ๋ฆฌํ๋ฉด,
- sort()์ ๊ฒฝ์ฐ ์คํ ๊ฒฐ๊ณผ๋ก ๋ฐํ๋๋ ๋ฐฐ์ด๊ณผ ๋ฉ์๋๋ฅผ ์คํํ ๋ฐฐ์ด์ด ๋ชจ๋ ์ ๋ ฌ์ด ๋๋ ๋ฐ๋ฉด, toSorted()์ ๊ฒฝ์ฐ์๋ ๋ฐํ๋๋ ๋ฐฐ์ด๋ง ์ ๋ ฌ๋๋ค๋ ์
- toSpliced()๋ ์๋ณธ ๋ฐฐ์ด์ ์์ ํ์ง ์๊ณ ์ ๊ฑฐ๋ ๋ถ๋ถ์ผ๋ก ์๋ก์ด ๋ฐฐ์ด ๋ฐํ
- reverse()๋ In-Place ๋ฐฉ์์ผ๋ก ๋์ํ๋ฉฐ ๋์ ๋ฐฐ์ด์ ๋์ด ์์๋ฅผ ๋ค์ง๋ ๋ฐ๋ฉด, toReversed()๋ ์๋ณธ ๋ฐฐ์ด๊ณผ ๋ค๋ฅธ ์๋ก์ด ๋ฐฐ์ด์ ๋ฐํ
2. with
with(๋ณ๊ฒฝํ๋ ค๋ ์ธ๋ฑ์ค์, ๊ฐ)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/with
Array.prototype.with() - JavaScript | MDN
The with() method of Array instances is the copying version of using the bracket notation to change the value of a given index. It returns a new array with the element at the given index replaced with the given value.
developer.mozilla.org
์ด๋ค ๋ฐฐ์ด์ ์์ ํด์ ํ์ฉํ๊ณ ์ ํ ๋ ์ด์ฉํ๋ ๋ฉ์๋์ด๋ฉฐ,
์๋ ์์ ๋ ๋ฐฐ์ด์ ๊ฒฐ๊ณผ๋ฅผ ์ป๊ณ ์ ํ ๋, ์๋ณธ ๋ฐฐ์ด๋ ํจ๊ป ์์ ๋์๋ค.
with()๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐฐ์ด์์ ํ ์์๋ง ๋ค๋ฅธ ๊ฐ์ผ๋ก ๋ณ๊ฒฝํ๊ณ ์ถ์ ๋, ์ฌ์ฉํ ์ ์์.
3. find~
โ findLast, findLastIndex
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast
Array.prototype.findLast() - JavaScript | MDN
The findLast() method iterates the array in reverse order and returns the value of the first element that satisfies the provided testing function. If no elements satisfy the testing function, undefined is returned.
developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex
Array.prototype.findLastIndex() - JavaScript | MDN
The findLastIndex() method iterates the array in reverse order and returns the index of the first element that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.
developer.mozilla.org
๊ธฐ์กด์ find, findIndex์ ๋์๋์ด ๋์จ ๋ฌธ๋ฒ์ธ๋ฐ,
last๋ ์ด๋ฆ์์ ์ ์ถํด๋ณผ ์ ์๋ฏ์ด ๋ค์์๋ถํฐ ์ธ๋ฑ์ค๋ฅผ ์ฐพ๋๋ค.
find, findIndex ๋ฉ์๋๋ ์์์ ๋ถํฐ ํ์ํด์ ๊ฐ์ฅ ๋จผ์ ๋์ค๋ ๊ฑธ ์ฐพ์๊ธฐ ๋๋ฌธ์
๋ฐฐ์ด์ ๋ง์ง๋ง๋ถํฐ ์ฐพ์ผ๋ ค๋ฉด ๋ฐฐ์ด์ ๋ค์ง์ด์ ๋ณต์ฌํ๊ฑฐ๋ ํ๋ ๊ณผ์ ์ด ํ์ํ๋๋ฐ,
findLast, findLastIndex๋ฉ์๋๋ฅผ ์ด์ฉํ๋ฉด ๊ทธ๋ฌ์ง ์์๋ ๋๋ค.
findLast( x => x === ์ฐพ์ ๊ฐ) // ์ฝ๋ฐฑํจ์๋ฅผ ์ธ์๋ก ๋ฃ์ด์ค๋ค
findLastIndex( x => x === ์ฐพ์ ๊ฐ)
'์ฝ๋ฉ ๋ฉ๋ชจ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
border line ์์ผ๋ก ์ค์ ํ๊ธฐ (0) | 2023.06.19 |
---|---|
[error] '~' cannot be used as a JSX component. (0) | 2023.06.16 |
[error] key๊ฐ์ ๋ฃ์ด์ฃผ์ง ์์ ์๊ธฐ๋ ์ค๋ฅ (4) | 2023.05.23 |
[error] <input>์ value์์ฑ์ด ๊ณ ์ ๊ฐ์ด ์๋๋ผ ๋๋ ์๋ฌ (0) | 2023.05.23 |
[error] Parsing error: 'import' and 'export' may only appear at the top level. (0) | 2023.05.20 |