프로그래머스 배열 원소의 길이 자바스크립트 | map(), reduce()

Chaeyeon Lee·2023년 5월 19일
0

🔅 1. 아이디어

새로운 배열에다가 strlist각 원소 방문하면서 길이 재고 저장해서 넣어야지

🧑‍💻 2. 내 코드

function solution(strlist) {
    let arr=[0, ];
    for(let i=0; i<strlist.length; i++){
        arr[i]=strlist[i].length;
    }
    return arr;
}

🐣 3. 개념

📌 Array.prototype.map()

map() 개념

function solution(strlist) {
    return strlist.map((el) => el.length)
}

배열 돌면서 특정 행위를 할 때에는 map을 쓰는 게 좋다...

📌 Array.prototype.reduce()

reduce() 개념

function solution(strlist) {
    return strlist.reduce((a, b) => [...a, b.length], [])
}

이렇게 reduce를 이용해도 된다! 배열도 반환값이 될 수 있다는 걸 항상 까먹는다.

profile
프론트엔드 개발자 지망생

0개의 댓글