글이 너무 길어져서 배열의 메소드는 따로 정리했다.
*맨 앞에서 요소를 추가할 때 unshift()
Array.prototype.unshift()메소드는 배열의 맨 앞에 1개 이상의 요소를 추가하고
배열의 새로운 길이를 반환할 수 있다. unshift는 배열의 길이를 return한다.
1
2
3
4
5
|
const array = [1,2,3,4];
const count = array.unshift(0);
console.log(count); //5
console.log(array); //[0,1,2,3,4]
|
cs |
*맨 앞에서 요소를 지울 때 shift()
Array.prototype.shift()메소드는 배열 내에서 0번째 인덱스에 오는 첫 번째 요소를 제거해
나머지 값의 위치를 한 칸씩 앞으로 오게끔 만든다.
매개변수가 없으며 제거된 첫번째 요소를 return한다.
1
2
3
4
5
|
const array = [1,2,3,4];
const element = array.shift();
console.log(element); //output : 1
console.log(array); //output : [2,3,4]
|
cs |
*맨 뒤에서 요소를 추가할 때 push()
Array.prototype.push()메소드는 배열의 맨 끝에 1개 이상의 요소를 추가하고
배열의 소로운 길이를 반환한다.
1
2
3
4
5
|
const array = [1,2,3,4];
const count = array.push(5);
console.log(count); //output : 5
console.log(array); //output : [1,2,3,4,5]
|
cs |
*맨 뒤에서 요소를 제거할 때 pop()
Array.prototype.pop()메소드는 배열 내에 존재하는 요소의 맨 마지막 요소를 제거하고
해당 요소를 return한다.
빈 배열에서 사용할 경우 undefined를 return한다.
1
2
3
4
5
|
const array = [1,2,3,4];
const element = array.pop();
console.log(element); //output : 4
console.log(array); //output : [1,2,3]
|
cs |
*★특정한 인덱스에 요소를 위치시킬 때 splice()
Array.prototype.splice()메소드는 배열 내에 새로운 요소의 추가가 가능하며
원하는 인덱스에 위치가 가능하다. 또한 기존에 존재하는 요소를 삭제하거나 변경할 수 있다.
1
2
|
//구조
arrayName.splice(target_index, remove_count, insert_element);
|
cs |
target_index는 insert_element를 삽입하고자 하는 위치값이다.
배열보다 큰 값을 입력하면 마지막 인덱스로 간주한다.
음수를 입력 시 뒤에서부터 접근한다.
remove_count는 제거할 요소의 개수를 정할 수 있다.
아무것도 지우고 싶지 않으면 0 이하의 숫자를 입력한다.
1
2
3
4
5
6
|
const array = [1,2,3,4];
array.splice(1,0,5);
console.log(array); //output : [1,5,2,3,4]
const array = [1,2,3,4];
array.splice(-1,1,5); //output : [1,2,3,5]
|
cs |
*특정 요소를 제거할 때에도 splice()
1
2
|
//구조
arrayName.splice(target_index, remove_count);
|
cs |
배열 내에 존재하는 기존 요소를 제거만 하고자 할 때에 세 번째 매개변수를 생략한 상태에서 사용한다.
*배열 일부분을 잘라내서 새로운 배열로 반환할 때 slice()
Array.prototype.slice()메소드는 원본을 변경하는 메소드가 아니며, 원본 배열 요소의 얕은 복사본을 반환한다.
1
2
|
//구조
arrayName.slice(start_index, end_index);
|
cs |
start_index는 잘라낼 배열의 시작점의 인덱스이다. 음수로 입력하면 뒤에서부터 접근한다.
start_index가 undefined 일 경우에는 0번 인덱스부터 접근하고
배열 길이보다 큰 경우 빈 배열을 반환한다.
end_index는 잘라낼 배열의 종료지점 인덱스이며, 잘라낼 배열에 포함되지 않는다.
즉 slice(1, 3)이면 1 <= index < 3 으로 인식하면 편하다.
음수를 입력할 시 배열의 끝에서부터 접근하여 end_index를 정한다.
end_index를 생략할 시 start_index 부터 마지막 index까지가 범위로 지정된다.
1
2
3
4
|
const array = ['one', 'two', 'three', 'four'];
console.log(array.slice(1, 3)); //output : ['two', 'three'], range : 1 <= index < 3
console.log(array.slice(-2, -1)); //output : ['three'], range : 2 <= index < 3
console.log(array.slice(0, -2)); //output : ['one', 'two'], range : 0 <= index < 2
|
cs |
*순서를 거꾸로 뒤집고 싶다면 reverse()
Array.prototype.reverse()메소드는 배열 내 요소의 순서를 거꾸로 뒤집고 마지막 위치에 있는 인덱스의
요소가 0번째 위치에 오게 된다.
해당 메소드는 원폰 배열을 변형시키고 그 참조를 반환한다.
1
2
3
|
const array = [1,2,3,4,5];
array.reverse();
console.log(array); //output : [5,4,3,2,1]
|
cs |
*요소의 인덱스를 찾고 싶을 때 indexOf()
Array.prototype.indexOf()메소드는 배열에서 지정한 요소를 찾아 인덱스를 탐색한다.
배열에 존재하지 않는 요소를 찾거나 해당 인덱스에서 지정된 요소가 탐색되지 않을 시
-1 값을 return한다.
1
2
|
//구조
arrayName.indexOf(element, start_index)
|
cs |
element는 탐색하고자 하는 요소를 입력하고
start_index는 탐색을 시작하고 하는 인덱스를 입력하며 start_index는 생략가능하다.
1
2
|
const array = [1,2,3,4,5];
console.log(array.indexOf(3)); //output : 2
|
cs |
*인자가 배열인지 확인하고 싶을 때 isArray()
Array.isArray()메소드는 입력받은 매개변수가 배열인지 아닌지 여부를 판별해 boolean 값으로 return한다.
*요소들을 연결해 하나의 값으로 만들 때 join()
Array.prototype.join()메소드는 배열 내 원소들을 연결해 하나의 값으로 만들며
그 값의 자료형은 문자열이다. 매개변수를 생략하면 쉼표로 구분되며, 빈 문자열을 넣을 시
띄어쓰기 없이 연결된다.
1
2
3
|
const array = ['one', 'two', 'three', 'four', 'five'];
console.log(array.join('/')); //output : 'one/two/three/four/five'
console.log(array.join()); //output : 'onetwothreefourfive'
|
cs |
너무 길어서 다음 글에 이어서 작성하겠습니다.