배열 특정 위치(index) 찾기
배열 객체의 메소드
indexOf() & lastIndexOf()
1. indexOf()
: indexOf (searchElement [, fromIndex])
는 배열에서 searchElement를 검색하고 첫 번째 일치 항목의 인덱스를 반환합니다.
var a = ['a', 'b', 'a', 'b', 'a'];
console.log(a.indexOf('b')); // logs 1
// Now try again, starting from after the last match
console.log(a.indexOf('b', 2)); // logs 3
console.log(a.indexOf('z')); // logs -1, because 'z' was not found
2. lastIndexOf()
: lastIndexOf(searchElement[, fromIndex])
메서드는 indexOf메서드와 유사하게 작동하지만 배열의 뒤쪽에서부터 요소를 찾습니다.
var a = ['a', 'b', 'c', 'd', 'a', 'b'];
console.log(a.lastIndexOf('b')); // logs 5
// Now try again, starting from before the last match
console.log(a.lastIndexOf('b', 4)); // logs 1
console.log(a.lastIndexOf('z')); // logs -1
Author And Source
이 문제에 관하여(배열 특정 위치(index) 찾기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@gunu/배열-특정-위치index-찾기저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)