웹페이지에서 데이터를 가지고 있을때,

input 박스에 검색어를 입력하고 검색을 할때 유용하게 사용 할 수 있다.

 

 

사용 방법>

"검색할 대상 문자열".indexOf("검색 키위드")

일치하는 키워드가 있다면 첫 index의 위치값을 반환하고,

일치하는 검색 키워드가 없다면 -1 을 돌려준다.

 

사용 예제>
var str = "가나다라마바사";

var searchStr = "라마";

if (str.indexOf("라마") != -1) {

    console.log("데이터 있음");

}
else {
    console.log("데이터 없음");
}

 

 

출처>

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

 

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

 

+ Recent posts