프론트/JS)코딩테스트
JS) 배열에 있는 요소를 문자열 배열로
뽀짝코딩
2024. 9. 19. 22:30
728x90
1번
const array = [3, 4, 5, 2, 1];
// 각 요소를 문자열로 변환하여 새로운 배열 생성
const stringArray = array.map(num => num.toString());
console.log(stringArray); // ["3", "4", "5", "2", "1"]
map으로 배열을 돌면서
num.toString() 을 호출해 숫자를 문자열로 변환.
2번- 더 간단한 방법❗❗❗
const array = [3, 4, 5, 2, 1];
const stringArray = [...array.join('')];
// console.log(stringArray);
[ '3', '4', '5', '2', '1' ]
반응형