๐Ÿ† Javascript(1) ์ฃผ์š” ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ๋ฒ•

Lee Jooamยท2022๋…„ 4์›” 27์ผ
0
post-custom-banner

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” prototype์ด๋ผ๋Š” ๊ฐœ๋…์ด ์žˆ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด ๊ธฐ์กด ๊ฐ์ฒด์˜ ์›ํ˜•์ธ๋ฐ, ์ด prototype ๊ฐ์ฒด์— ๋‚ด์žฅ๋œ ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์„ ํŒŒ์ƒ๋œ ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ชจ๋“  ๋ฉ”์†Œ๋“œ๋“ค์„ ์™ธ์šฐ๊ธฐ๋ž€ ํ˜„์‹ค์ ์œผ๋กœ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ˆ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋“ค์„ ์ •๋ฆฌํ•ด๋ณด๊ฒ ๋‹ค.

String

1. slice

const str = "Hello World!!";

console.log(str.slice(0, 6));
console.log(str.slice(0, -2));
console.log(str);
Hello 
Hello World
Hello World!!

slice๋Š” ๋ฌธ์ž์˜ ์ผ๋ถ€๋ถ„์„ ์ž˜๋ผ returnํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ์ธ์ˆ˜๋กœ start, end ์ธ๋ฑ์Šค๋ฅผ ๋ฐ›์œผ๋ฉฐ end๋กœ ๋ฐ›์€ ์ธ๋ฑ์Šค๋Š” ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค.

end ๋ฐ”๋กœ ์ง์ „ index๊นŒ์ง€๋งŒ ํฌํ•จํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜ผ๋™ํ•˜์ง€ ์•Š๋„๋ก ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋กœ ์Œ์ˆ˜ ๊ฐ’๋„ ๊ฐ€๋Šฅํ•œ๋ฐ ๊ฐ€์žฅ ๋ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋‚ด๋ ค๊ฐ€๋Š” ํ˜•์‹์ด๋‹ค.

2.indexOf

const str = "Hello World!!";

console.log(str.indexOf("H"));
console.log(str.indexOf("World"));
console.log(str.indexOf("Good"));
console.log(str.indexOf("o", 6));
console.log(str);
0
6
-1
7
Hello World!!

indexOf๋Š” ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ํ•ด๋‹น String์„ ํฌํ•จํ•  ๊ฒฝ์šฐ ์ฒ˜์Œ์œผ๋กœ ์ฐพ์€ ์ธ๋ฑ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ position ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, position ์œ„์น˜๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ผ๋Š” ์˜๋ฏธ๊ฐ€ ๋œ๋‹ค.

3. replace

replace๋„ ์ง๊ด€์ ์ด๋‹ค. ํŠน์ • ํŒจํ„ด์˜ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๋ฉด ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ๋กœ ๋งŒ๋‚˜๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ›์€ ์ธ์ž๋กœ ๊ต์ฒดํ•œ๋‹ค.

const str = "Hello World!!, Hello!!";

console.log(str.replace(/Hello/g, "Goodbye"));
console.log(str.replace("Hello", "Goodbye"));
console.log(str);
Goodbye World!!, Goodbye!!
Goodbye World!!, Hello!!
Hello World!!, Hello!!

ํƒ€๊ฒŸ ๋ฌธ์ž์—ด์— ์ •๊ทœํ‘œํ˜„์‹์„ ๋„ฃ์–ด์„œ ํ™œ์šฉํ•˜๋ฉด ์ฒซ ๋ฒˆ์งธ๋กœ ๋งŒ๋‚˜๋Š” ๋ฌธ์ž์—ด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋งค์นญ๋˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๊ต์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค.

4. split

const str = "abcdefgh";

console.log(str.split(""));
console.log(str.split("e"));
console.log(str.split("e", 1));
console.log(str);
[
  'a', 'b', 'c',
  'd', 'e', 'f',
  'g', 'h'
]
[ 'abcd', 'fgh' ]
[ 'abcd' ]
abcdefgh

split์€ ๋งค์šฐ ์œ ์šฉํ•˜๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋“ค์–ด์˜จ seperator๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ์ชผ๊ฐ ๋‹ค.

split์„ ์ด์šฉํ•˜๋ฉด ๋ฐฐ์—ด๋กœ ๋ณ€ํ•œ ๋ฌธ์ž์—ด ๋ฐ์ดํ„ฐ์— ๋ฐฐ์—ด ๋ฉ”์†Œ๋“œ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ํฐ ์žฅ์ ์ด ์žˆ๋‹ค.

๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ limit๋ฅผ ๋ฐ›๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ ํ•ด๋‹น ์ˆ˜ ๋งŒํผ ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋ฅผ ์ œํ•œํ•œ๋‹ค.

Number

1. isInteger

const fNum = 123.3;
const iNum = 123;

console.log(Number.isInteger(fNum));
console.log(Number.isInteger(iNum));
false
true

์ธ์ž๋กœ ๋“ค์–ด์˜จ ์ˆซ์ž๊ฐ€ ์ •์ˆ˜์ธ์ง€ ํŒ๋ณ„ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ๋‹ค๋งŒ ๋ฉ”์†Œ๋“œ์˜ ํ˜ธ์ถœ์„ Number ๋ž˜ํผ ๊ฐ์ฒด์ธ ๊ฒƒ์„ ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

2. toString

const fNum = 123.3;
const iNum = 123;

console.log(fNum.toString());
console.log(typeof fNum.toString());
console.log(iNum.toString(2));
123.3
string
1111011

toString ๋ฉ”์†Œ๋“œ๋Š” ํ•ด๋‹น number๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์ด๋•Œ ์ธ์ž๋กœ radix๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์œ„์˜ ๊ฒฐ๊ณผ์ฒ˜๋Ÿผ 2๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ์ •์ˆ˜๊ฐ€ ์ด์ง„๋ฒ•์œผ๋กœ ๋ณ€ํ™˜๋œ string์ด ๋ฐ˜ํ™˜๋œ๋‹ค.

3. parseInt, parseFloat

const num = "123";
const fNum = "123.456";

console.log(Number.parseInt(num));
console.log(Number.parseInt(fNum));
console.log(Number.parseFloat(fNum));
console.log(parseInt(fNum));
console.log(parseFloat(fNum), "\n");

console.log(typeof Number.parseInt(num));
console.log(typeof Number.parseInt(fNum));
console.log(typeof Number.parseFloat(fNum));
console.log(typeof parseInt(fNum));
console.log(typeof parseFloat(fNum));
123
123
123.456
123
123.456

number
number
number
number
number

parseInt์™€ parseFloat์€ ์ธ์ž๋กœ ๋ฐ›์€ string์„ number๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค. ๋‹ค๋งŒ parseInt๋Š” ์†Œ์ˆ˜์  ์•„๋ž˜ ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ํ•ด์•„ํ•œ๋‹ค.

๋‘ ๋ฉ”์†Œ๋“œ๋Š” Number ๊ฐ์ฒด๊ฐ€ ํ˜ธ์ถœํ•ด๋„ ๋˜๊ณ  ๋”ฐ๋กœ ํ˜ธ์ถœ์ž ์—†์ด ์‚ฌ์šฉ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

Math ๊ฐ์ฒด

const num = -123;
const fNum = 123.456;
const arr = [1, 2, 3, 4, 5];

console.log(Math.abs(num));
console.log(Math.max(...arr));
console.log(Math.min(...arr));
console.log(Math.ceil(fNum));
console.log(Math.floor(fNum));
console.log(Math.round(fNum));
console.log(Math.random());
123
5
1
124
123
123
0.26679628239106457

Math ๊ฐ์ฒด๋Š” ์ˆ˜ํ•™๊ณผ ๊ด€๋ จ๋œ ๋ฉ”์†Œ๋“œ๋“ค์„ ๋‹ด๋‹นํ•œ๋‹ค. ์ ˆ๋Œ€๊ฐ’, ์ตœ๋Œ€, ์ตœ์†Œ, ์˜ฌ๋ฆผ, ๋‚ด๋ฆผ, ๋ฐ˜์˜ฌ๋ฆผ, ๋žœ๋ค ๋“ฑ ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.

์ด ์™ธ์—๋„ sqrt, log ๋“ฑ ์ˆ˜ํ•™์ ์ธ ๋ฉ”์†Œ๋“œ๋“ค์„ ์ฐพ๋Š”๋‹ค๋ฉด Math ๊ฐ์ฒด๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒŒ ์ข‹๋‹ค.

Array

๋ฐฐ์—ด ๊ด€๋ จ ๋ฉ”์†Œ๋“œ๋“ค์€ ์ •๋ง ๋งŽ์ด ์“ฐ์ธ๋‹ค. ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์ด ์—ฌ๋Ÿฌ ๊ตฐ๋ฐ์—์„œ ์“ฐ์ด๋Š” ๋งŒํผ ์ˆ™๋‹ฌ๋  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

1. find, findIndex, includes,

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log(arr.find((item) => item > 5));
console.log(arr.find((item) => item > 10));
console.log(arr.findIndex((item) => item > 5));
console.log(arr.findIndex((item) => item > 10));
console.log(arr.includes(1));
console.log(arr.includes(10));
6
undefined
5
-1
true
false

Array๋Š” ๋‹ค์–‘ํ•œ find ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค. find์™€ findIndex ๋ชจ๋‘ ํŒ๋ณ„์„ ์œ„ํ•œ callback์„ ์ธ์ž๋กœ ๋ฐ›๋Š”๋ฐ, ์œ„์˜ ๊ฒฐ๊ณผ์ฒ˜๋Ÿผ ์—ญํ• ์€ ๋น„์Šทํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

find๋Š” ์กฐ๊ฑด์— ๋ถ€ํ•ฉํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์กฐ๊ฑด์— ๋ชจ๋‘ ๋ถ€ํ•ฉํ•˜์ง€ ์•Š์œผ๋ฉด undefined, findIndex๋Š” ์ธ๋ฑ์Šค์™€ -1์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.

includes ๋ฉ”์†Œ๋“œ๋Š” ๋ฐฐ์—ด์ด ์ธ์ž๋ฅผ ํฌํ•จํ•˜๋ฉด true, ์•„๋‹ˆ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

2. slice, map, filter, concat

const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

console.log(arr.slice(0, 4));
console.log(arr.slice(10, 15));
console.log(arr.map((data) => data * 2));
console.log(arr.filter((data) => data >= 5));
console.log(arr.concat([1,2,3,4]));
[ 1, 2, 3, 4 ]
[]
[
   2,  4,  6,  8, 10,
  12, 14, 16, 18
]
[ 5, 6, 7, 8, 9 ]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4]

์œ„์˜ ๋ฉ”์†Œ๋“œ๋“ค์€ ๋ฐฐ์—ด ์›๋ณธ์„ ๋ฐ”๊พธ์ง€ ์•Š๋Š”๋‹ค. ๋Œ€์‹  ํ•ด๋‹น ๋ฐฐ์—ด์˜ ์›์†Œ๋“ค์„ ์กฐ์ž‘ํ•œ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

slice๋Š” String ๊ฐ์ฒด์˜ slice์™€ ์œ ์‚ฌํ•˜๋‹ค. ์ธ๋ฑ์Šค ๋ฒ”์œ„๊ฐ€ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๋„˜์–ด์„œ๋ฉด ๋นˆ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

map๊ณผ filter๋Š” ์‚ฌ์šฉ๋ฐฉ๋ฒ•์ด ๋น„์Šทํ•˜๋‹ค.

map์€ ์›์†Œ๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ์ˆœํšŒํ•˜๋ฉฐ ํ•ด๋‹น ์›์†Œ์— ํŠน์ˆ˜ํ•œ ์กฐ์ž‘์„ ๊ฐ€ํ•˜๊ณ , filter๋Š” ์›๋ณธ ๋ฐฐ์—ด์˜ ์›์†Œ๋“ค ์ค‘ ์ฃผ์–ด์ง„ ํŒ๋ณ„ callback์— ๋ถ€ํ•ฉํ•˜๋Š” ๊ฒƒ๋“ค๋งŒ ์ถ”๋ฆฐ๋‹ค.

concat์€ ๋ฐฐ์—ด์— ์ƒˆ๋กœ์šด ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค. ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์›๋ณธ ๋ฐฐ์—ด์€ ๋ฐ”๋€Œ์ง€ ์•Š๋Š”๋‹ค.

4. splice, push, pop, shift, unshift

์ด๋ฒˆ ๋ฉ”์†Œ๋“œ๋“ค์€ ์›๋ณธ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์˜๋„ํ•˜์ง€ ์•Š์€ ๊ฒฐ๊ณผ๋ฅผ ๋‚ณ์„ ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์— ์ถฉ๋ถ„ํžˆ ์ฃผ์˜ํ•˜์—ฌ์•ผ ํ•œ๋‹ค.

const arr = [1, 2, 3, 4, 5];

console.log("splice", arr.splice(3, 2), "\n");
console.log("After splice", arr, "\n");
console.log("push", arr.push(...[10, 11, 12, 13]), "\n");
console.log("After push", arr, "\n");
console.log("pop", arr.pop(), "\n");
console.log("After pop", arr, "\n");
console.log("shift", arr.shift(), "\n");
console.log("After shift", arr, "\n");
console.log("unshift", arr.unshift(100, 101), "\n");
console.log("After unshift", arr, "\n");
splice [ 4, 5 ]
After splice [ 1, 2, 3 ]
push 7
After push [
   1,  2,  3, 10,
  11, 12, 13
]
pop 13
After pop [ 1, 2, 3, 10, 11, 12 ]
shift 1
After shift [ 2, 3, 10, 11, 12 ]
unshift 7
After unshift [
  100, 101,  2, 3,
   10,  11, 12
]

splice๋Š” ์‹œ์ž‘ ์ธ๋ฑ์Šค, ๊ทธ๋ฆฌ๊ณ  ์นด์šดํ„ฐ๋ฅผ ๋ฐ›๋Š”๋‹ค.

์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ์นด์šดํ„ฐ๊นŒ์ง€ ์ž˜๋ผ๋‚ธ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์ž˜๋ฆฐ ๊ฒฐ๊ณผ๋Š” ์›๋ณธ ๋ฐฐ์—ด์—๋„ ๋ฐ˜์˜๋œ๋‹ค.

push์™€ pop์€ ๊ฐ๊ฐ ์‚ฝ์ž…, ์ œ๊ฑฐ ๋ฉ”์†Œ๋“œ์ด๋‹ค.

push๋Š” ๋ฐฐ์—ด์— ์›์†Œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , pop์€ ์ œ๊ฑฐ๋œ ์›์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

shift์™€ unshift๋Š” push, pop๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ push, pop์ด ๋ฐฐ์—ด์˜ ๊ผฌ๋ฆฌ์—์„œ ์ผ์–ด๋‚œ๋‹ค๋ฉด, shift, unshift๋Š” ๋ฐฐ์—ด์˜ ๋จธ๋ฆฌ์—์„œ ์ผ์–ด๋‚œ๋‹ค๋Š” ์ ์ด ๋‹ค๋ฅด๋‹ค.

๋ฐ˜ํ™˜ ๊ฐ’์€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ๊ฐ ์ƒˆ๋กœ์šด ๊ธธ์ด, ์ œ๊ฑฐ๋œ ์›์†Œ์ด๋‹ค.

5. reserve, sort

const arr = [1, 2, 3, 4, 5];

console.log(arr.reverse());
console.log(arr);
console.log(arr.sort((a, b) => a - b));
console.log(arr);
[ 5, 4, 3, 2, 1 ]
[ 5, 4, 3, 2, 1 ]
[ 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5 ]

reverse์™€ sort ๋˜ํ•œ ์›๋ณธ ๋ฐฐ์—ด์„ ์ˆ˜์ •ํ•œ๋‹ค. reverse๋Š” ๋‹จ์ˆœํžˆ ๋ฐฐ์—ด์„ ์—ญ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  sort๋Š” ์ฃผ์ž…๋œ callback์— ๋”ฐ๋ผ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•œ๋‹ค.

const arr = [50000, 19999, 30000, 1000000];

console.log(arr.sort());

console.log(
  arr.sort((a, b) => {
    if (a >= 30000) return -1;
    else {
      a - b;
    }
  })
);
[ 1000000, 19999, 30000, 50000 ]
[ 50000, 30000, 1000000, 19999 ]

๋‹ค๋งŒ sort์˜ callback์ธ compareFunc์˜ ์‚ฌ์šฉ์— ์ฃผ์˜ํ•˜์—ฌ์•ผ ํ•˜๋Š”๋ฐ, ๊ธฐ๋ณธ ๊ฐ’์€ ๋ฌธ์ž์˜ ์œ ๋‹ˆ์ฝ”๋“œ ๊ธฐ์ค€ ์ •๋ ฌ์ด๋‹ค.

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ ์˜ˆ์‹œ์—์„œ 1,000,000์ด ๊ฐ€ ์•ž์œผ๋กœ ์˜ค๊ฒŒ ๋œ๋‹ค. Number ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋„ ๋ฌธ์ž ๊ธฐ์ค€ ์ •๋ ฌ์ด๋ฏ€๋กœ ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

compareFunc์€ ์Œ์ˆ˜, 0, ์–‘์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ a, b๋ฅผ ๋น„๊ตํ•ด ์Œ์ˆ˜ ๊ฐ’์ด ๋ฐ˜ํ™˜๋œ ๊ฒฝ์šฐ a๋ฅผ b๋ณด๋‹ค ๋‚ฎ์€ ์ธ๋ฑ์Šค๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.

๋ฐ˜๋Œ€๋ผ๋ฉด b๊ฐ€ a๋ณด๋‹ค ๋‚ฎ์€ ์ธ๋ฑ์Šค๋กœ ์ •๋ ฌ๋œ๋‹ค. 0์ด๋ผ๋ฉด ์„œ๋กœ๋ฅผ ์ •๋ ฌํ•˜์ง€ ์•Š๊ณ  ๋‹ค์Œ ์›์†Œ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

const arr = [3, 6, 10000, 5250, 6];

console.log(arr.sort());
console.log(arr.sort((a, b) => a - b));
console.log(arr.sort((a, b) => b - a));
[ 10000, 3, 5250, 6, 6 ] // ๋ฌธ์ž ์œ ๋‹ˆ์ฝ”๋“œ ๊ฐ’ ๊ธฐ์ค€
[ 3, 6, 6, 5250, 10000 ] // ์˜ค๋ฆ„์ฐจ์ˆœ
[ 10000, 5250, 6, 6, 3 ] // ๋‚ด๋ฆผ์ฐจ์ˆœ

๋‹ค์Œ๊ณผ ๊ฐ™์ด compareFunc์„ ์ ์ ˆํ•˜๊ฒŒ ์‘์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

6. forEach, reduce

const arr = [1, 2, 3, 4, 5];
let result = 0;

for (let i = 0; i < arr.length; i++) {
  result += arr[i];
}

console.log(result);
arr.forEach((item) => (result += item));
console.log(result);
15
30

forEach ๋ฉ”์†Œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์›์†Œ๋งˆ๋‹ค callback์„ ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰ํ•œ๋‹ค. ์ด๋•Œ map์ฒ˜๋Ÿผ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒŒ ์•„๋‹Œ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const arr = [1, 2, 3, 4, 5];

console.log(arr.reduce((acc, curr) => acc + curr));
console.log(arr.reduce((acc, curr) => acc + curr, 100));
15
115

reduce๋Š” ์œ ์šฉํ•œ ๋ฉ”์†Œ๋“œ์ด๋‹ค. ์ธ์ž๋กœ callback๊ณผ accumulator๋ฅผ ๋ฐ›๋Š”๋‹ค.

accumulator๋Š” ์ฒซ ํ˜ธ์ถœ ๋•Œ ์„ค์ •ํ•œ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์„ค์ •ํ•œ ๊ฐ’์„ ๋ฐ›๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ ์›์†Œ๊ฐ€ ๋œ๋‹ค.

์ฒซ ํ˜ธ์ถœ ์ดํ›„๋ถ€ํ„ฐ๋Š” callback์—์„œ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’๋“ค์ด accumulator๊ฐ€ ๋œ๋‹ค.

const arr = [1, 2, 3, 4, 5];
let result = 0;

console.log(
  arr.reduce((acc, curr) => {
    acc.push(curr * 2);

    return acc;
  }, [])
);
[ 2, 4, 6, 8, 10 ]

reduce๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด map ํ•จ์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋Š” ๋“ฑ ์‚ฌ์šฉ์ž์˜ ์˜๋„์— ๋”ฐ๋ผ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

profile
ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž๋กœ ๊ฑธ์–ด๊ฐ€๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.
post-custom-banner

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