์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ๊ณต๋ถํ๋ค๊ฐ slice,splice,split์ ์ฐจ์ด๊ฐ ๋~๋ฌด ํท๊ฐ๋ฆฌ๋ ๊ฒ์ด๋ค. ์์ด๋จ์ด๋ ๋น์ทํ๊ฒ ์๊ฒจ๊ฐ์ง๊ท..๐ญ
๊ทธ๋์ ์ด๋ฒ์ ์ ๋ฆฌํด์ ๋จธ๋ฆฟ์์ ์ง์ด ๋ฃ์ด์ผ๊ฒ ๋ค~~!! ์ค๋์ ์ธ๋ ๊ฐ๋ฐ์์ ๋์์ ๋ฐ์๋ค! ๊ธ๋ก๋ฒํ ๊ฐ๋ฐ์ ์ธ๊ณ><
์ฐ์ method type์ผ๋ก ์ด ์ ์ ๋๋ ์ ์๋ค.
Array method: Slice() and Splice()
String method: Split()
slice()๋ ๋ฐฐ์ด ๋ฉ์๋๋ก ์ํ๋ ๋ถ๋ถ์ ๋ณต์ฌํ๊ณ ์๋ก์ด ๋ฐฐ์ด๋ก ๋ฆฌํดํ๋ค.
๋ฐฐ์ด์ ๋ณ๊ฒฝํ์ง ์๋ ๊ฒ์ด ํต์ฌ์ด๋ค.
์๋ ์์๋ฅผ ์ดํด๋ณด์.
//loaf3๊ณผ loaf5๋ฅผ ์ ๋ฐฐ์ด์ ๋ณต์ฌํ๊ณ ์ถ์ด์ง..
let bread=['loaf1','loaf2','loaf3','loaf4','loaf5','loaf6'];
newArr=bread.slice(2,5);
//output:["loaf3", "loaf4", "loaf5"]
Splice() ๋ํ ๋ฐฐ์ด ๋ฉ์๋๋ก ๋ฐฐ์ด์ ์ํ๋ ์๋ฆฌ๋จผํธ ๊ฐฏ์๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํ ์ ์๋ค. ์ด๋ ๊ธฐ์กด ๋ฐฐ์ด์ ๋ณ์ด๋ฅผ ์ค๋ค.
let bread = ['loaf1','loaf2','loaf3','loaf4','loaf5','loaf6'];
bread.splice(1, 3);
["loaf2", "loaf3", "loaf4"]
//output:["loaf1", "loaf5", "loaf6"]
2.์ ๊ฑฐํ๊ณ ์๋ก์ด ์๋ฆฌ๋จผํธ๋ฅผ ์ถ๊ฐํ๋ ๊ฒฝ์ฐ
let bread = ['loaf1','loaf2','loaf3','loaf4','loaf5','loaf6'];
bread.splice(1, 3, 'newLoaf1', 'newLoaf2');
console.log(bread);
//output:["loaf1", "newLoaf1", "newLoaf2", "loaf5", "loaf6"]
3.์ถ๊ฐ๋ง ํ๋ ๊ฒฝ์ฐ
let bread = ['loaf1','loaf2','loaf3','loaf4','loaf5','loaf6'];
bread.splice(3, 0, 'newLoaf1', 'newLoaf2');
console.log(bread);
//output:["loaf1", "loaf2", "loaf3", "newLoaf1", "newLoaf2", "loaf4", "loaf5", "loaf6"]
Split()์ string ๋ฉ์๋์ง๋ง ๋ฐฐ์ด์ ๋ฆฌํดํ๋ค. ๋ฌธ์์ด์ ๋ฐฐ์ด๋ก ์ ํํ ๋ ๋งค์ฐ ์ ์ฉํ๋ค.
์์๋ฅผ ์ดํด๋ณด์ฅ!
let myString = "the quick brown fox jumps over the lazy dog";
//For a comma separated array use " ":
myString.split(" ");
//output: ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"]
//For each alphabet as element of array use "":
myString.split("");
//output: ["t", "h", "e", " ", "q", "u", "i", "c", "k", " ", "b", "r", "o", "w", "n", " ", "f", "o", "x", " ", "j", "u", "m", "p", "s", " ", "o", "v", "e", "r", " ", "t", "h", "e", " ", "l", "a", "z", "y", " ", "d", "o", "g"]
๐ ์์๋๋ฉด ์ข์ ํ
.toString()
์ ์ฌ์ฉํ์ฌ string์ผ๋ก ์ ํํด๋ผ.var myString = array.toString();
2.string์ ๋ฐฐ์ด๋ก ์ ํํ๊ธฐ์ํด ("")์ ํจ๊ป split์ ์ฌ์ฉํด๋ผ
myString.split(" ");
๊ฒฐ๋ก
๋ง์ด ์จ๋ด์ผ ๋ค์ ํ์คํ๊ฒ ์ฐจ์ด์ ์ ์ตํ ์ ์์ ๊ฒ ๊ฐ๋ค!
์ ๋ฆฌ๋ฅผ ํ๋๊น ๋ ์ดํด๊ฐ ๋๋๊ตฐ~!
์ฐธ๊ณ :https://medium.com/@anupamroy8/slice-vs-splice-vs-split-in-javascript-344c41cb94a5