• 회원가입
  • 로그인
  • 구글아이디로 로그인

[Array] JS - lastIndexOf() 메서드(배열) ★ - 마지막일치배열값위치찾기 (= 배열값포함검사 = 마지막일치 배열순번 = 라스트인덱스어브)

목차
  1. array.lastIndexOf() 예제 - 마지막 일치 배열값 위치 찾기
  2. array.lastIndexOf() 정의
  3. array.lastIndexOf() 구문
  4. array.lastIndexOf() 예제 - 찾을 시작위치가 양수인 경우
  5. array.lastIndexOf() 예제 - 찾을 시작위치가 음수인 경우

 

array.lastIndexOf() 예제 - 마지막 일치 배열값 위치 찾기

 

<button onclick="homzzang()">클릭</button>


<p id="demo"></p>


<script>

function homzzang() {

  var code = ["HTML","CSS","JS","JQ","PHP","SQL","HTML","BS"];

  var a = code.lastIndexOf("HTML");

  var x = document.getElementById("demo");

  x.innerHTML = a;

}

</script>

 

결과값: 6


array.lastIndexOf() 정의

 

지정 배열값의 마지막 색인번호 반환.

 


 

1.

  • 지정 배열값이 존재 X 경우, -1 반환.
  • 지정 색인위치부터 시작해 오른쪽에서 왼쪽으로 검색.
  • 기본적으로, 검색은 배열 마지막값부터 시작해 배열 시작값에서 종료.
  • 시작값이 음수인 경우엔, 시작위치를 맨뒤부터 기산함. (이때도 여전히 검색방향은 오른쪽에서 왼쪽임.

 

2. cf.

array.indexOf() 메서드 - 처음 일치 배열값 위치 찾기

 

3.

  • ECMAScript5 (ES5: JavaScript 2009)
  • IE9 이상 주요 최신 브라우저 모두 지원.

 

4. MDN array.lastIndexOf() 예제보기

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf

 

 

array.lastIndexOf() 구문

 

array.lastIndexOf(value, start)

 


[매개변수]

 

value

필수. 찾을 배열값

 

start

선택. 검색 시작위치 (기본값: array.length-1)

  • 양수: 검색 시작위치를 앞에서부터 계산. (※ 0: 맨앞)
  • 음수: 검색 시작위치를 뒤에서부터 계산. (※ -1: 맨뒤)
  • 양수든 음수든 검색방향은 뒤에서 앞으로 함.

 


[반환값]

 

  • 발견 O 시, 마지막 일치 배열값 색인번호 반환.
  • 발견 X 시, -1 반환.

 

 

array.lastIndexOf() 예제 - 찾을 시작위치가 양수인 경우

(예) 색인번호 4부터 시작해 왼쪽으로 HTML 검색.

 

<p id="demo"></p>


<script>

const langs = ["HTML","CSS","JS","HTML","jQuery","HTML"];

let index = langs.lastIndexOf("HTML", 4);


document.getElementById("demo").innerHTML = index;

</script>

 

결과값: 3

 

array.lastIndexOf() 예제 - 찾을 시작위치가 음수인 경우

(예) 맨뒤 2번째 시작해 왼쪽으로 HTML 검색.

 

<p id="demo"></p>


<script>

const langs = ["HTML","CSS","JS","HTML","jQuery","HTML"];

let index = langs.lastIndexOf("HTML", -2);


document.getElementById("demo").innerHTML = index;

</script>

 

결과값: 3



분류 제목
Basic JS - Let 키워드 ★★★★★ - 변수 블럭범위 설정 (렛키워드) ※ 클로저
Modernizr JS - Modernizr (모더나이저 = 마더나이저) - 크로스브라우징 구현 (= 브라우저의 HTML5 C…
React JS - React (리액트) - JS 라이브러리 일종으로 사용자 인터페이스 구축에 사용.(= 현재시간 = …
DOM_Attribute JS - Attribute 객체 - 속성객체
Window_Console JS - window.console 객체 - 콘솔객체
DOM_Document JS - Document 객체 - 웹문서 객체의 속성/메서드 종류 (= document객체 = 다큐먼트객체)
DOM_Element JS - Element 객체 - 요소객체 (속성 + 메서드) 종류
DOM_Event JS - Event 종류 ★ - 이벤트부착 / 이벤트종류 / 이벤트속성 / 이벤트메서드
API_Geolocation JS - Geolocation - 지리위치객체종류
Window_History JS - window.history 객체 정의 + 히스토리 객체의 (속성/메서드) 종류
DOM_HTMLCollection JS - HTMLCollection 객체 (속성 + 메서드) 종류
Window_Location JS - window.location 객체 - 위치 객체 (= window.location객체 = 윈도우로케…
Window_Navigator JS - window.navigator 객체 - 네비게이터 객체 (= window.navigator객체 = …
Window_Screen JS - window.screen 객체 - 스크린 객체 (= window.screen객체 = 윈도우스크린객체…
DOM_Style JS - CSS 스타일 속성값 반환 ★★ + style 객체의 속성 종류
Window_Object JS - window 객체 - 윈도우객체 (속성/메서드) 종류
API_Storage JS - Storage Object - 스토리지 객체 (※ 데이터 저장소)
Object JS - Object Constructor - 객체생성자종류 ★
Object JS - Object Prototypes - 객체프로토타입 (= 객체원형) ★
Object JS - object ECMAScript 5 - 객체혁명
22/67
목록
찾아주셔서 감사합니다. Since 2012