배열 객체의 메서드(2)

Gunwoo Kim·2021년 5월 13일
0

JavaScript

목록 보기
8/17
post-thumbnail

Array 객체의 메서드(2)

1. 배열 병합

1-1. concat()

: concat() 메서드는 두개의 배열을 합쳐 새로운 배열을 반환합니다.

var myArray = new Array('1', '2', '3');
myArray = myArray.concat('a', 'b', 'c');
// myArray is now ["1", "2", "3", "a", "b", "c"]

1-2. join()

: join(delimiter = ',') 메서드는 배열의 모든 요소를 주어진 구분자로 연결된 하나의 문자열을 반환 합니다.

var myArray = new Array('Wind', 'Rain', 'Fire');
var list = myArray.join(' - '); // list is "Wind - Rain - Fire"

2. 배열 추출

2-1. slice()

: slice(start_index, upto_index)메서드는 배열의 특정 부분을 추출하여 그 추출된 부분을 포함하는 새로운 배열을 반환 합니다. upto_index에 해당하는 요소는 포함되지 않습니다.

var myArray = new Array('a', 'b', 'c', 'd', 'e');
myArray = myArray.slice(1, 4); // starts at index 1 and extracts all elements
                               // until index 3, returning [ "b", "c", "d"]

2-2. splice()

: splice(index, count_to_remove, addElement1, addElement2, ...) 메세드는 주어진 인덱스 요소를 포함하여 count_to_remove 갯수만큼 삭제 하고 주어진 요소로 바꿔 줍니다.

var myArray = new Array('1', '2', '3', '4', '5');
myArray.splice(1, 3, 'a', 'b', 'c', 'd');
// myArray is now ["1", "a", "b", "c", "d", "5"]
// This code started at index one (or where the "2" was),
// removed 3 elements there, and then inserted all consecutive
// elements in its place.

0개의 댓글