Legend:
- ✏️
this(본래의 배열)을 수정합니다. - 🔒
this(본래의 배열)을 수정하지 않습니다.
Array<T>.prototype.*:
-
concat(...items: Array<T[] | T>): T[]🔒 ES3this와 모든items이 연계된 새로운 배열을 반환합니다. 배열이 아닌 파라미터는 단일 요소 배열인 것 처럼 처리됩니다.> ['a'].concat('b', ['c', 'd']) [ 'a', 'b', 'c', 'd' ] -
copyWithin(target: number, start: number, end=this.length): this✏️ ES6start에서end까지의 범위에 있는 인덱스의 요소를target으로 복사합니다. Overlapping은 바르게 처리됩니다.> ['a', 'b', 'c', 'd'].copyWithin(0, 2, 4) [ 'c', 'd', 'c', 'd' ] > [0, 1, 2, 3, 4, 5, 6, 7].copyWithin(0, 4, 6); [4, 5, 2, 3, 4, 5, 6, 7] -
entries(): Iterable<[number, T]>🔒 ES6Returns an iterable over [index, element] pairs. iterable한 [index, element] 배열 리스트를 요소로 갖는 배열을 반환합니다.
> Array.from(['a', 'b'].entries()) [ [ 0, 'a' ], [ 1, 'b' ] ] -
every(callback: (value: T, index: number, array: Array<T>) => boolean, thisArg?: any): boolean🔒 ES5모든 요소가
callback에서true를 반환하면true를 반환합니다.false를 받으면 중지됩니다. 수학의 ∀(All)> [1, 2, 3].every(x => x > 0) true > [1, -2, 3].every(x => x > 0) false -
fill(value: T, start=0, end=this.length): this✏️ ES6모든 index에
value를 할당합니다.> [0, 1, 2].fill('a') [ 'a', 'a', 'a' ] -
filter(callback: (value: T, index: number, array: Array<T>) => any, thisArg?: any): T[]🔒 ES5callback이ture인 요소들만 가진 배열을 반환합니다.> [1, -2, 3].filter(x => x > 0) [ 1, 3 ] -
find(predicate: (value: T, index: number, obj: T[]) => boolean, thisArg?: any): T | undefined🔒 ES6predicate가 참인 첫 번째 엘리먼트의 값을 반환합니다. 만약 일치하는 것이 없다면,undefined를 반환합니다.> [1, -2, 3].find(x => x < 0) -2 > [1, 2, 3].find(x => x < 0) undefined -
findIndex(predicate: (value: T, index: number, obj: T[]) => boolean, thisArg?: any): number🔒 ES6predicate가 참인 첫 번째 엘리먼트의 index를 반환합니다. 만약 일치하는 것이 없다면,-1을 반환합니다.> [1, -2, 3].findIndex(x => x < 0) 1 > [1, 2, 3].findIndex(x => x < 0) -1 -
forEach(callback: (value: T, index: number, array: Array<T>) => void, thisArg?: any): void🔒 ES5각각의 요소에 대하여
callback을 호출합니다.['a', 'b'].forEach((x, i) => console.log(x, i)) // 'a' 0 // 'b' 1
-
includes(searchElement: T, fromIndex=0): boolean🔒 ES2016searchElement가 어떠한 하나의 요소와 SameValueZero-equal 이면true를, 그렇지 않으면false를 반환합니다. SameValueZero-equal 란, 엄격한 동치를 의미하지만,NaN또한 그 자신과 동치입니다.> [0, 1, 2].includes(1) true > [0, 1, 2].includes(5) false -
indexOf(searchElement: T, fromIndex=0): number🔒 ES5searchElement와 엄격히 동일한 첫 번재 요소의 index를 반환합니다. 일치하는 값이 없으면-1을 반환합니다.fromIndex번째 index에서 검색을 시작해 다음 인덱스를 검색합니다.> ['a', 'b', 'a'].indexOf('a') 0 > ['a', 'b', 'a'].indexOf('a', 1) 2 > ['a', 'b', 'a'].indexOf('c') -1 -
join(separator = ','): string🔒 ES1모든 요소의 문자열 표현을 연결하여,
separator로 구분된 문자열을 만듭니다.> ['a', 'b', 'c'].join() 'a,b,c' > ['a', 'b', 'c'].join('##') 'a##b##c' -
keys(): Iterable<number>🔒 ES6반복시킬 수 있는 배열의 키(index)를 반환합니다.
> [...['a', 'b'].keys()] [ 0, 1 ] -
lastIndexOf(searchElement: T, fromIndex=this.length-1): number🔒 ES5searchElement와 엄격히 동치인 마지막 요소의 index를 반환합니다. 일치하는 값이 없으면-1을 반환합니다.fromIndex인덱스에서 검색을 시작하여, 다음 인덱스를 검색합니다.> ['a', 'b', 'a'].lastIndexOf('a') 2 > ['a', 'b', 'a'].lastIndexOf('a', 1) 0 > ['a', 'b', 'a'].lastIndexOf('c') -1 -
map<U>(callback: (value: T, index: number, array: ReadonlyArray<T>) => U, thisArg?: any): U[]🔒 ES5callback의 결과에 해당하는 요소들을 갖는 새로운 배열을 반환합니다.> [1, 2, 3].map(x => x * 2) [ 2, 4, 6 ] > ['a', 'b', 'c'].map((x, i) => i) [ 0, 1, 2 ] -
pop(): T | undefined✏️ ES3배열의 마지막 요소를 제거하고 반환합니다. 즉, 배열의 끝을 스택으로 처리합니다.
> const arr = ['a', 'b', 'c']; > arr.pop() 'c' > arr [ 'a', 'b' ] -
push(...items: T[]): number✏️ ES30개 이상의
items을 배열의 끝에 추가합니다. 즉, 배열의 끝을 스택으로 처리합니다. 반환값은 추가한 변경후의 배열의 length값 입니다.> const arr = ['a', 'b']; > arr.push('c', 'd') 4 > arr [ 'a', 'b', 'c', 'd' ] -
reduce<U>(callback: (state: U, element: T, index: number, array: T[]) => U, firstState?: U): U🔒 ES5callback은 현재 상태와, 배열의 요소가 정해지면 다음 상태를 계산합니다..reduce()는 배열 요소에 인덱스 0 부터 시작하여 앞으로 진행됩니다.firstState가 제공되지 않으면, 배열의 0번째 index가 대신 사용됩니다. 마지막 상태는.reduce()의 결과입니다.> [1, 2, 3].reduce((state, x) => state + String(x), '') '123' > [1, 2, 3].reduce((state, x) => state + x, 0) 6역자주 : 보다 상세한 내용은 [이 글](https://css-tricks.com/understanding-the-almighty-reducer/)을 읽어 보세요. -
reduceRight<U>(callback: (state: U, element: T, index: number, array: T[]) => U, firstState?: U): U🔒 ES5.reduce()와 유사하지만, 마지막 요소로부터 첫 요소로 거꾸로 진행합니다.> [1, 2, 3].reduceRight((state, x) => state + String(x), '') '321' -
reverse(): this✏️ ES1배열의 요소를 역순으로 정렬해 반환합니다.
> const arr = ['a', 'b', 'c']; > arr.reverse() [ 'c', 'b', 'a' ] > arr [ 'c', 'b', 'a' ] -
shift(): T | undefined✏️ ES3배열의 첫 번째 요소를 제거 후 반환합니다.
.unshift()와 반대됩니다.> const arr = ['a', 'b', 'c']; > arr.shift() 'a' > arr [ 'b', 'c' ] -
slice(start=0, end=this.length): T[]🔒 ES3index가
start와end사이의 요소들인 새로운 배열을 반환합니다.> ['a', 'b', 'c', 'd'].slice(1, 3) [ 'b', 'c' ] > ['a', 'b'].slice() // 얕은 복사 [ 'a', 'b' ] -
some(callback: (value: T, index: number, array: Array<T>) => boolean, thisArg?: any): boolean🔒 ES5callback이 적어도 하나의 요소에 대해true를 반환하면true를 반환합니다.true를 만나면 중지됩니다. 수학: ∃(exist)> [1, 2, 3].some(x => x < 0) false > [1, -2, 3].some(x => x < 0) true -
sort(compareFn?: (a: T, b: T) => number): this✏️ ES1배열을 정렬해
this를 반환합니다. 정렬 순서는 다음과 같은 수를 반환하는compareFn을 통해 지정됩니다:- Negative if
a < b(mnemonic: negative = less than zero) - Zero if
a === b - Positive if
a > b
> [3, 1, 2].sort((a, b) => a - b) [ 1, 2, 3 ] > ['b', 'a', 'c'].sort((a, b) => a < b ? -1 : a > b ? +1 : 0) [ 'a', 'b', 'c' ] - Negative if
-
splice(start: number, deleteCount=this.length-start, ...items: T[]): T[]✏️ ES3start인덱스부터deleteCount까지의 요소에items를 삽입(덮어쓰기)합니다. 삭제된 요소들을 반환합니다.> const arr = ['a', 'b', 'c', 'd']; > arr.splice(1, 2, 'x', 'y') [ 'b', 'c' ] > arr [ 'a', 'x', 'y', 'd' ] -
toString(): string🔒 ES1모든 요소의 문자열 표현을 쉼표(
,)로 구분한 문자열을 반환합니다.> [1, 2, 3].toString() '1,2,3' > ['a', 'b', 'c'].toString() 'a,b,c' > [].toString() '' -
unshift(...items: T[]): number✏️ ES3이 배열의 시작부분에
items를 삽입하고, 수정한 배열의 길이를 반환합니다.> const arr = ['c', 'd']; > arr.unshift('e', 'f') 4 > arr [ 'e', 'f', 'c', 'd' ]
- Array methods of various ECMAScript versions in detail: http://exploringjs.com
- Holes and array methods: Sect. “Array operations and holes” in “Exploring ES6”.
- https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts
- MDN
- ECMAScript spec