맨λ μ°λ©΄μλ μ΄λ¦μ΄ λΉμ·ν΄μ κ·Έλ°κ° μ μΈ λ©μλκ° μ λ¦¬κ° μλκ³ ν·κ°λ €μ μ΄ μ°Έμ νλ² μμνκ² μ 리ν΄λ³΄μ~!
1. slice
Array.prototype.slice() λλ String.prototype.slice()
`slice()` λ©μλλ λ¬Έμμ΄ λλ λ°°μ΄μμ νΉμ λ²μμ μμλ€μ 볡μ¬νμ¬ μλ‘μ΄ λ¬Έμμ΄ λλ λ°°μ΄μ λ°ννλ€.
- Array
// μμ μΆμ²: MDN web docs - Array.prototype.slice()
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"]
console.log(animals.slice(-2));
// Expected output: Array ["duck", "elephant"]
console.log(animals.slice(2, -1));
// Expected output: Array ["camel", "duck"]
console.log(animals.slice());
// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]
- String
// μμ μΆμ²: MDN web docs - String.prototype.slice()
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
// Expected output: "the lazy dog."
console.log(str.slice(4, 19));
// Expected output: "quick brown fox"
console.log(str.slice(-4));
// Expected output: "dog."
console.log(str.slice(-9, -5));
// Expected output: "lazy"
2. splice
Array.prototype.splice()
`splice()` λ©μλλ λ°°μ΄μμ νΉμ λ²μμ μμλ€μ μ κ±°νκ±°λ κ΅μ²΄νλ©°, νμν κ²½μ° μ μμλ₯Ό μΆκ°ν μ μλ€.
// μμ μΆμ²: MDN web docs - Array.prototype.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"]
3. split
String.prototype.split()
`split()` λ©μλλ λ¬Έμμ΄μ νΉμ ꡬλΆμλ₯Ό κΈ°μ€μΌλ‘ λΆν νμ¬ λ°°μ΄λ‘ λ°ννλ€.
// μμ μΆμ²: MDN web docs - String.prototype.split()
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."]
4. κ·Έλμ μ°¨μ΄μ μ΄ λλ°?
- slice()λ λ¬Έμμ΄κ³Ό λ°°μ΄ λͺ¨λμμ μ¬μ©ν μ μλ€. μμλ₯Ό 볡μ¬νμ¬ λ°ννλ©°, μλ³Έμ λ³κ²½λμ§ μλλ€.
- splice()λ λ°°μ΄μμ μμλ₯Ό μ κ±°νκ±°λ κ΅μ²΄νλ©°, μλ³Έ λ°°μ΄μ΄ λ³κ²½λλ€.
- split()μ λ¬Έμμ΄μμ μ¬μ©νλ©°, ꡬλΆμλ₯Ό κΈ°μ€μΌλ‘ λΆν νμ¬ μλ‘μ΄ λ°°μ΄λ‘ λ°ννλ€.
- μΈ λ©μλλ λͺ¨λ μλ³Έ λ°μ΄ν°μ μν₯μ λ―ΈμΉλ λ°©μκ³Ό λ°ννλ κ°μ ννμμ μ°¨μ΄κ° μλ€.
- slice()μ split()μ λ°νλ κ°μ΄ μλ‘μ΄ λ°μ΄ν°λ‘ κΈ°μ‘΄ λ°μ΄ν°μ μν₯μ μ£Όμ§ μμ λ€λ£¨κΈ° μ½λ€λ μ₯μ μ΄ μλ€.
- λ°λ©΄, splice()λ μλ³Έ λ°°μ΄μ μ§μ μμ νκΈ° λλ¬Έμ μ£Όμν΄μ μ¬μ©ν΄μΌ νλ€.
ꡬκΈλ§νλ€κ° λ¨λ²μ μ΄ν΄κ° κ°λ κ·Έλ¦Όμ΄ μμ΄μ ν¨κ» 첨λΆ!
'π» Programming > JS, TS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[JavaScript] μ΄λ²€νΈ κ°μ²΄ (0) | 2024.01.13 |
---|---|
[JavaScript] window&document μ μκ°μ²΄μ DOM (0) | 2024.01.11 |
[TypeScript] Map (0) | 2023.07.27 |
[JavaScript] μλ°μ€ν¬λ¦½νΈλ‘ ν¬λ¦¬μ€λ§μ€κΉμ§ λ¨μ μκ° κ³μ°νκΈ° (0) | 2023.06.21 |
[JavaScript] var, let, const (0) | 2023.06.20 |