๐Ÿ—ก์ž๋ฅด๊ธฐ ๋ฉ”์†Œ๋“œ ์ •๋ฆฌ๐Ÿ—ก

9rganizedChaosยท2021๋…„ 1์›” 26์ผ
0
post-thumbnail

๐Ÿ”ชArray.prototype.slice()

(๐ŸŽ์›๋ณธ ๋ฐฐ์—ด ์œ ์ง€๐ŸŽ)

  • slice() ๋ฉ”์„œ๋“œ๋Š” ์–ด๋–ค ๋ฐฐ์—ด์˜ begin๋ถ€ํ„ฐ end๊นŒ์ง€(end ๋ฏธํฌํ•จ)์— ๋Œ€ํ•œ ์–•์€ ๋ณต์‚ฌ๋ณธ์„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๊ฐ์ฒด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์›๋ณธ ๋ฐฐ์—ด์€ ๋ฐ”๋€Œ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]

๐Ÿ”ชArray.prototype.splice()

(๐Ÿ‰์›๋ณธ ๋ฐฐ์—ด ๋ณ€๊ฒฝ๐Ÿ‰)

  • splice() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ธฐ์กด ์š”์†Œ๋ฅผ ์‚ญ์ œ ๋˜๋Š” ๊ต์ฒดํ•˜๊ฑฐ๋‚˜ ์ƒˆ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]

๐Ÿ”ชString.prototype.split()

(๐ŸŽ์›๋ณธ ๋ฌธ์ž์—ด ์œ ์ง€๐ŸŽ)

  • split() ๋ฉ”์„œ๋“œ๋Š” String ๊ฐ์ฒด๋ฅผ ์ง€์ •ํ•œ ๊ตฌ๋ถ„์ž๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ๋‚˜๋ˆ•๋‹ˆ๋‹ค.
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
console.log(words[3]);
// expected output: "fox"

const chars = str.split('');
console.log(chars[8]);
// expected output: "k"

const strCopy = str.split();
console.log(strCopy);
// expected output: Array ["The quick brown fox jumps over the lazy dog."]

๐Ÿ”ชString.prototype.substring()

(๐ŸŽ์›๋ณธ ๋ฌธ์ž์—ด ์œ ์ง€๐ŸŽ)

  • substring()๋ฉ”์†Œ๋“œ๋Š” string ๊ฐ์ฒด์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋กœ ๋ถ€ํ„ฐ ์ข…๋ฃŒ ์ธ๋ฑ์Šค ์ „ ๊นŒ์ง€ ๋ฌธ์ž์—ด์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
const str = 'Mozilla';

console.log(str.substring(1, 3));
// expected output: "oz"

console.log(str.substring(2));
// expected output: "zilla"

๐Ÿ”ชString.prototype.substr()

(๐ŸŽ์›๋ณธ ๋ฌธ์ž์—ด ์œ ์ง€๐ŸŽ)

  • substr() ๋ฉ”์„œ๋“œ๋Š” ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ํŠน์ • ๋ฌธ์ž ์ˆ˜ ๋งŒํผ์˜ ๋ฌธ์ž๋“ค์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
const str = 'Mozilla';

console.log(str.substr(1, 2));
// expected output: "oz"

console.log(str.substr(2));
// expected output: "zilla"
profile
๋ถ€์ •ํ™•ํ•œ ์ •๋ณด๋‚˜ ์ž˜๋ชป๋œ ์ •๋ณด๋Š” ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๋น ๋ฅด๊ฒŒ ์ˆ˜์ •ํ† ๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค, ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

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