728x90
반응형
indexOf(), lastIndexOf(), includes()
indexOf() / lastIndexOf() / includes()
indexOf()메서드는 배열 요소를 검색할 때 사용하며 숫자로 반환합니다. lastIndexOf()메서드는 배열 요소를 끝에서 검색하여 숫자로 반환합니다. includes()메서드는 배열 요소를 검색하여 불린(true, false)으로 반환합니다.
번호 | 기본값 | 메서드 | 리턴값 |
---|---|---|---|
js
const arrNum = [100, 200, 300, 400, 500];
const arrIndex = arrNum.indexOf(200);
document.querySelector(".sample10_N1").innerHTML = "1";
document.querySelector(".sample10_Q1").innerHTML = "[100, 200, 300, 400, 500]";
document.querySelector(".sample10_M1").innerHTML = "arrNum.indexOf(200)";
document.querySelector(".sample10_P1").innerHTML = arrIndex;
const arrNum2 = [100, 200, 300, 400, 500];
const arrIndex2 = arrNum2.indexOf(300);
document.querySelector(".sample10_N2").innerHTML = "2";
document.querySelector(".sample10_Q2").innerHTML = "[100, 200, 300, 400, 500]";
document.querySelector(".sample10_M2").innerHTML = "arrNum.indexOf(300)";
document.querySelector(".sample10_P2").innerHTML = arrIndex2;
const arrNum3 = [100, 200, 300, 400, 500];
const arrIndex3 = arrNum3.indexOf(600);
document.querySelector(".sample10_N3").innerHTML = "3";
document.querySelector(".sample10_Q3").innerHTML = "[100, 200, 300, 400, 500]";
document.querySelector(".sample10_M3").innerHTML = "arrNum.indexOf(600)";
document.querySelector(".sample10_P3").innerHTML = arrIndex3;
const arrNum4 = [100, 200, 300, 400, 200];
const arrIndex4 = arrNum4.lastIndexOf(200);
document.querySelector(".sample10_N4").innerHTML = "4";
document.querySelector(".sample10_Q4").innerHTML = "[100, 200, 300, 400, 200]";
document.querySelector(".sample10_M4").innerHTML = "arrNum.lastindexOf(200)";
document.querySelector(".sample10_P4").innerHTML = arrIndex4;
const arrNum5 = [100, 200, 300, 400, 200];
const arrIndex5 = arrNum5.includes(200);
document.querySelector(".sample10_N5").innerHTML = "5";
document.querySelector(".sample10_Q5").innerHTML = "[100, 200, 300, 400, 200]";
document.querySelector(".sample10_M5").innerHTML = "arrNum.includes(200)";
document.querySelector(".sample10_P5").innerHTML = arrIndex5;
const arrNum6 = [100, 200, 300, 400, 200];
const arrIndex6 = arrNum6.includes(600);
document.querySelector(".sample10_N6").innerHTML = "6";
document.querySelector(".sample10_Q6").innerHTML = "[100, 200, 300, 400, 200]";
document.querySelector(".sample10_M6").innerHTML = "arrNum.includes(600)";
document.querySelector(".sample10_P6").innerHTML = arrIndex6;
728x90
반응형
'javascript' 카테고리의 다른 글
JavaScript filter(), map(), Array.from() (1) | 2022.09.27 |
---|---|
JavaScript find(), findIndex() (1) | 2022.09.27 |
JavaScript slice(), splice() (2) | 2022.09.27 |
JavaScript reduce(), reduceRight() (3) | 2022.09.27 |
JavaScript concat() (2) | 2022.09.27 |