JS소스모듈

리스트안에서 엘리먼트의 인덱스 번호 찾기

JoyYellow 2022. 11. 9. 14:36
const findIndexListElement = (element) => {
    const listItems = element.parentElement.querySelectorAll('li');
    
    //findIndex : 주어진 판별 함수를 만족하는 배열의 첫 번째 요소에 대한 인덱스를 반환
    const currentIndex = Array.prototype.slice.call(listItems).findIndex(listItem => listItem === element);

    return currentIndex;
}

export default findIndexListElement;

출처: https://github.com/paullabkorea/theJungleFinalCodingTestFrontEnd