Array APIs - join(), split()

2022. 10. 6. 12:00Javascript

이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

반응형

join : 배열을 원하는 구분자를 더해서 문자열로 반환

join(separator?: string): string;
/**
 * Reverses the elements in an array in place.
 * This method mutates the array and returns a reference to the same array.
 */
const arr = ['Lorem', 'ipsum', 'dolor', 'sit'];
const result = arr.join('');
console.log(result); //Loremipsumdolorsit

const result = arr.join(' ');
console.log(result); //Lorem ipsum dolor sit

 

split :  구분자를 통해, 문자열을 배열로 반환

split(separator: string | RegExp, limit?: number): string[];
/**
 * Split a string into substrings using the specified separator and return them as an array.
 * @param separator A string that identifies character or characters to use in separating the string. If omitted, a single-element array containing the entire string is returned.
 * @param limit A value used to limit the number of elements returned in the array.
 */
const str = 'Lorem ipsum dolor sit, amet consectetur adipisicing elit.';
const result = str.split(' ');
console.log(result); //['Lorem', 'ipsum', 'dolor', 'sit,', 'amet', 'consectetur', 'adipisicing', 'elit.']

const str = 'car, bus, subway, airplane';
const result = str.split(', ');
console.log(result); //['car', 'bus', 'subway', 'airplane']

 

반응형