자바스크립트 map 특정 인덱스 건너뛰기

이석원·2022년 7월 28일
2

map 을 써서 과제를 하던 중 특정 인덱스를 건너뛰어서 함수를 적용해야했다. 코드는 다음과 같다.

const parsing = (arr)=>{
    const result = []
    for(let i=1; i<arr.length; i++){
        const a = {}
        const key = arr[0].split(",");
        const temp = arr[i].split(",");
        temp.map((e,index)=>{if(index === key.length-1) return; a[key[index].trim()] = e})
        result.push(a);
    }
    return result;
}

temp.map((e,index)=>{if(index === key.length-1) return; a[key[index].trim()] = e}) 를 다음에 쓰기 좋게 일반화 해보고자 한다.

arr.map((element, index)=>{if(index === 10)return; callback()})

사용 예제)

arr = [0,1,2,3,4,5,6,7,8,9];
arr.map((element, index)=>{if(index === 2)return; console.log(element)})

잘 작동한다.

profile
개발자 공부중

1개의 댓글

comment-user-thumbnail
2022년 7월 28일

오! 대박 좋은거 알게되었습니다. 감사합니다👍👍

답글 달기