[JavaScript] 배열

dadev·2021년 8월 9일
0

JavaScript

목록 보기
1/5
post-thumbnail

✍객체의 유형

배열, 함수, 배열이나 함수가 아닌 객체

✍배열에서 요소 제거하는 방법

👉pop() 사용


const target=['가','나','다','라','마'];
target.pop();
console.log(target);

👉splice() 사용


const target = ['가','나','다','라','마'];
target.splice(1);
console.log(target);


const target = ['가','나','다','라','마'];
target.splice(1,3,'타','파');
console.log(target);

=> 인덱스 1부터 3개 요소를 먼저 제거하고 요소를 제거한 자리에 '타', '파' 채우기

👉배열에서 요소 찾기


const target = ['가','나','다','라','마'];
const result = target.includes('다');
const result2 = target.includes('카');
console.log(result);
console.log(result2);

=>includes에 주어진 값이 배열에 존재하면 true, 아니면 false

👉검색하고 싶은 값이 몇 번째 인덱스에 위치하는지 확인하는 법 => indexOf, lastaindexOf


const target = ['가','나','다','라','마'];
const result = target.indexOf('다');
const result2 = target.lastIndexOf('라');
const result3 = target.indexOf('가');
console.log(result);
console.log(result2);
console.log(result3);

=> 배열에 '라', '다' 두 개씩 존재

indexOf : 앞에서부터 주어진 값 find

lastIndexOf : 뒤에서부터 주어진 값 find

'라' -> 인덱스 0,3에 위치하지만 lastIndexOf는 뒤에서부터 찾으므로 결괏값 : 3

result 3 : 배열 안에 존재하지 않는 값의 인덱스를 찾으면 결괏값 : -1

profile
매일매일 최선을 다하는 개발자

0개의 댓글