반응형

//11. 인덱스 몇 번째에 있는지 찾기 indexOf

//알파벳 찾기
console.clear();
let abcArr1 = "ABCDEFGHIJKMN";
console.log('---------------------------------------------------- J의 인덱스는?');
console.log(abcArr1.indexOf('J')); // 9 

//역순으로 찾기 --> lastIndexOf();
let abcArr2 = "ABCDEFGHIJKJMN";

console.log('---------------------------------------------------- J의 역순 인덱스는?');
console.log(abcArr2.lastIndexOf('J')); // 11

//대소문자 구분을 한다. (Default)
let abcArr3 = "ABCDEFGHIJKJMN";
console.log('---------------------------------------------------- J의 인덱스는? 대소문자 구분');
console.log(abcArr3.lastIndexOf('j')); // -1

//대(소)문자 변환 후 --> indexOf 사용
let abcArr4 = "abcdABCD";
console.log('---------------------------------------------------- 대소문자 변환');
console.log(abcArr4.lastIndexOf('A')); // 4
console.log(abcArr4.toUpperCase().indexOf('A')); //0 
console.log(abcArr4.toUpperCase().lastIndexOf('A')); // 4

//13. indexOf 옵션 --> indexOf("찾을 특정 값(value)", "시작할 위치(position)")
console.clear();
console.log('---------------------------------------------------- 시작 위치 옵션');
const nums = ['k','o','r','e','a','u','s','a'];
console.log(nums.indexOf('a')); //4 
console.log(nums.indexOf('a', 2)); //4
console.log(nums.indexOf('a', 6)); //7

console.log('---------------------------------------------------- 시작 위치 옵션 - 문자열');
const strs13 = "Hello, World~!!!";

console.log(strs13.indexOf('o')); //4
console.log(strs13.indexOf('o', 5)); //8
console.log(strs13.indexOf('o', 9)); //-1

//14-1. 특정 번째에 있는 값의 인덱스 찾기 -->  세번째 4 찾기
console.log('---------------------------------------------------- 특정 번째 인덱스 찾기');
const numbers = [2,3,4,3,5,8,5,3,4,8,4,2,1];
console.log(numbers);

console.log(numbers.indexOf(4));    // 2
console.log(numbers.indexOf(4, 3)); // 8
console.log(numbers.indexOf(4, 9)); // 10

//14-2. 특정 번째에 있는 값의 인덱스 찾기 -->  세번째 3 찾기
console.log(numbers.indexOf(3));    // 1
console.log(numbers.indexOf(3, numbers.indexOf(3)+1)); // 3
console.log(numbers.indexOf(3, numbers.indexOf(3, numbers.indexOf(3)+1)+1)); // 7

반응형

'개발 및 관리 > Javascript' 카테고리의 다른 글

include, join, reverse  (0) 2023.11.18
숫자, 문자 찾기  (0) 2023.11.18
배열 붙이기 concat() 메서드  (0) 2023.10.31
splice() 메서드, splice, splice 메서드  (1) 2023.10.29
jQuery Quick API Reference  (0) 2023.02.01