Array APIs

Shin Woohyun·2021년 7월 28일
0

? 전달해도 되고 안 해도 된다.
T 배열?
S true,false
https://hyunseob.github.io/2017/01/14/typescript-generic/

join

join(separator?: string): string;
Adds all the elements of an array separated by the specified separator string.
배열의 모든 요소를 연결해 하나의 string을 만듭니다.

split

split(separator: string | RegExp, limit?: number): string[];
Split a string into substrings using the specified separator and return them as an array.

reverse

reverse(): T[];
변화된 배열을 리턴함.

splice

splice(start: number, deleteCount?: number): T[];
Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
배열 자체를 수정.

slice

slice(start?: number, end?: number): T[];
start 포함, end 미포함.


find

find<S extends T>(predicate: (this: void, value: T, index: number, obj: T[]) => value is S, thisArg?: any): S | undefined;
Returns the value of the first element in the array where predicate is true, and undefined
콜백함수가 배열의 모든 요소들마다 순차적으로 하나하나씩 호출이 되는데, 콜백함수의 리턴값이 true이면 바로 해당 요소를 리턴한다.

filter

filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[];
Returns the elements of an array that meet the condition specified in a callback function.
콜백함수의 리턴값이 true인 경우의 요소들을 array로 리턴한다.

map

map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
Calls a defined callback function on each element of an array, and returns an array that contains the results.
기존 배열 안의 모든 요소들을 콜백함수로 가공한 요소들로 대체한 새로운 배열을 리턴한다.

some

some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean;
콜백함수의 결과가 하나라도 true라면 true가 리턴된다.

every (모두 true여야함.)

every<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[];
콜백함수의 결과가 하나라도 false면 false가 리턴된다.

reduce

reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T;
배열의 요소가 curr에 순차적으로 전달되고,
누적값이 prev에 전달된다. (처음은 초기값)

sort

sort(compareFn?: (a: T, b: T) => number): this;
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

https://youtu.be/3CUjtKJ7PJg

0개의 댓글