본문으둜 κ±΄λ„ˆλ›°κΈ°

πŸ“„ String Operation

1. String을 μ§€μ •λœ 횟수만큼 반볡 μ‘°μž‘β€‹

λ¬Έμžμ—΄μ„ μ§€μ •λœ 횟수만큼 λ°˜λ³΅ν•˜λŠ” ν•¨μˆ˜λ₯Ό 섀계해 λ³΄μ„Έμš”.

I. repeat() solution(ES6+) μ‚¬μš©β€‹

ν˜„μž¬ String은 repeat()λ₯Ό μ§€μ›ν•˜λ―€λ‘œ 직접 μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

const repeatedString = 'Pitt';

console.log(`Name Repeat : ${repeatedString.repeat(3)}`); // "Name Repeat : PittPittPitt"

Reference​

String.prototype.repeat()

II. 루프 μ‚¬μš©β€‹

repeat()λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 경우, 루프λ₯Ό μ‚¬μš©ν•˜μ—¬ λ§€κ°œλ³€μˆ˜λ₯Ό 톡해 μ–‘μ˜ μ •μˆ˜ μ‘°κ±΄μ—μ„œλ§Œ λ™μž‘ν•˜λ„λ‘ μ œμ•½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

function repeatString(str, num) {
// μ–‘μ˜ μ •μˆ˜κ°€ μ•„λ‹Œμ§€ 검사
if (num < 0 || !Number.isInteger(num)) {
throw new Error('μ–‘μ˜ μ •μˆ˜λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”');
}

let repeatedString = '';
for (let i = 0; i < num; i++) {
repeatedString += str;
}
return repeatedString;
}

2. Stringμ—μ„œ 파일λͺ… λ˜λŠ” ν™•μž₯자 μ²˜λ¦¬β€‹

getFileExtension()을 μ„€κ³„ν•˜μ—¬ λ§€κ°œλ³€μˆ˜μ—μ„œ λ™μ˜μƒμ˜ ν™•μž₯자 ν˜•μ‹μ„ κ°€μ Έμ˜΅λ‹ˆλ‹€. ν™•μž₯μžκ°€ μ—†μœΌλ©΄ 파일λͺ…을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

const fileName = 'video.mp4';
const fileNameWithoutExtension = 'file';
const fileNameWithoutExtension2 = 'example.flv';
const fileNameWithoutExtension3 = 'movie.mov';
const fileNameWithoutExtension4 = '.gitignore';

I. split을 μ‚¬μš©ν•˜μ—¬ 파일λͺ… κ°€μ Έμ˜€κΈ°β€‹

const getFileExtension = (fileName) => {
const fileNameSplit = fileName.split('.');
return fileNameSplit[fileNameSplit.length - 1];
};

console.log(getFileExtension(fileName)); // "mp4"
console.log(getFileExtension(fileNameWithoutExtension)); // "file"
console.log(getFileExtension(fileNameWithoutExtension2)); // "flv"
console.log(getFileExtension(fileNameWithoutExtension3)); // "mov"
console.log(getFileExtension(fileNameWithoutExtension4)); // ""

3. λ°°μ—΄μ—μ„œ κ°€μž₯ κΈ΄ λ¬Έμžμ—΄ 찾기​

I. sort() λ©”μ„œλ“œ μ‚¬μš©β€‹

const stringArray = ['apple', 'banana', 'orange', 'kiwi', 'strawberry'];

const longestString = (stringArray) => {
return stringArray.sort((a, b) => b.length - a.length)[0];
};

console.log(longestString(stringArray)); // "strawberry"

II. reduce() λ©”μ„œλ“œ μ‚¬μš©β€‹

const stringArray = ['apple', 'banana', 'orange', 'kiwi', 'strawberry'];

const longestString = (stringArray) => {
return stringArray.reduce(
(acc, cur) => (acc.length > cur.length ? acc : cur),
''
);
};

console.log(longestString(stringArray)); // "strawberry"

4. λ¬Έμžμ—΄μ„ μΉ΄λ©œμΌ€μ΄μŠ€λ‘œ λ³€ν™˜β€‹

λ¬Έμžμ—΄μ„ μΉ΄λ©œμΌ€μ΄μŠ€λ‘œ λ³€ν™˜ν•˜λŠ” ν•¨μˆ˜λ₯Ό 섀계해 λ³΄μ„Έμš”.

I. replace() λ©”μ„œλ“œ μ‚¬μš©β€‹

const camelCase = (str) => {
return str.replace(/-([a-z])/g, (match, char) => char.toUpperCase());
};

console.log(camelCase('hello-world')); // "helloWorld"

II. split() λ©”μ„œλ“œ μ‚¬μš©β€‹

const camelCase = (str) => {
return str
.split('-')
.map((word, index) =>
index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1)
)
.join('');
};

console.log(camelCase('hello-world')); // "helloWorld"

5. λ°°μ—΄μ—μ„œ 쀑볡 λ¬Έμžμ—΄μ˜ μΆœν˜„ 횟수 찾기​

I. Map() λ©”μ„œλ“œ μ‚¬μš©β€‹

const stringArray = [
'apple',
'banana',
'orange',
'kiwi',
'strawberry',
'apple',
];

const countDuplicateString = (stringArray) => {
const map = new Map();
stringArray.forEach((item) => {
map.set(item, (map.get(item) || 0) + 1);
});
return Object.fromEntries(map);
};

console.log(countDuplicateString(stringArray)); // { apple: 2, banana: 1, orange: 1, kiwi: 1, strawberry: 1 }

II. reduce() λ©”μ„œλ“œλ‘œ 쀑볡 λ¬Έμžμ—΄ 횟수 찾기​

const stringArray = [
'apple',
'banana',
'orange',
'kiwi',
'strawberry',
'apple',
];

const countDuplicateString = (stringArray) => {
return stringArray.reduce((acc, cur) => {
acc[cur] = (acc[cur] || 0) + 1;
return acc;
}, {});
};

console.log(countDuplicateString(stringArray)); // { apple: 2, banana: 1, orange: 1, kiwi: 1, strawberry: 1 }

III. Object.groupBy() λ©”μ„œλ“œ μ‚¬μš©(ES2023+)​

const stringArray = ['apple', 'banana', 'orange', 'kiwi', 'strawberry'];

const countDuplicateString = (stringArray) => {
return Object.groupBy(stringArray);
};

console.log(countDuplicateString(stringArray)); // { apple: 2, banana: 1, orange: 1, kiwi: 1, strawberry: 1 }

6. λ°°μ—΄μ—μ„œ λ¬Έμžμ—΄μ˜ ν™•μž₯자λ₯Ό μ°Ύκ³  쀑볡 ν™•μž₯자 필터링​

I. split() λ©”μ„œλ“œ μ‚¬μš©β€‹

const files = [
'document.docx',
'image.jpg',
'script.js',
'style.css',
'data.json',
'image.png',
'new-image.png',
];

const getFileExtension = (files) => {
return files
.map((file) => file.split('.').pop())
.filter((file, index, self) => self.indexOf(file) === index);
};

console.log(getFileExtension(files)); // ["docx", "jpg", "js", "css", "json", "png"]