Skip to content

Instantly share code, notes, and snippets.

@DeadIntegral
Forked from rauschma/api-cheatsheet-array.md
Last active November 11, 2019 04:41
Show Gist options
  • Save DeadIntegral/d90e8532b9ca195603ad65b2e1ac2395 to your computer and use it in GitHub Desktop.
Save DeadIntegral/d90e8532b9ca195603ad65b2e1ac2395 to your computer and use it in GitHub Desktop.

Array<T>

Legend:

  • ✏️ this(본래의 배열)을 수정합니다.
  • 🔒 this(본래의 배열)을 수정하지 않습니다.

Array<T>.prototype.*:

  • concat(...items: Array<T[] | T>): T[] 🔒 ES3

    this와 모든 items이 연계된 새로운 배열을 반환합니다. 배열이 아닌 파라미터는 단일 요소 배열인 것 처럼 처리됩니다.

    > ['a'].concat('b', ['c', 'd'])
    [ 'a', 'b', 'c', 'd' ]
    
  • copyWithin(target: number, start: number, end=this.length): this ✏️ ES6

    start에서 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]> 🔒 ES6

    Returns 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[] 🔒 ES5

    callbackture 인 요소들만 가진 배열을 반환합니다.

    > [1, -2, 3].filter(x => x > 0)
    [ 1, 3 ]
    
  • find(predicate: (value: T, index: number, obj: T[]) => boolean, thisArg?: any): T | undefined 🔒 ES6

    predicate가 참인 첫 번째 엘리먼트의 값을 반환합니다. 만약 일치하는 것이 없다면, 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 🔒 ES6

    predicate가 참인 첫 번째 엘리먼트의 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 🔒 ES2016

    searchElement가 어떠한 하나의 요소와 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 🔒 ES5

    searchElement와 엄격히 동일한 첫 번재 요소의 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 🔒 ES5

    searchElement와 엄격히 동치인 마지막 요소의 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[] 🔒 ES5

    callback의 결과에 해당하는 요소들을 갖는 새로운 배열을 반환합니다.

    > [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 ✏️ ES3

    0개 이상의 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 🔒 ES5

    callback은 현재 상태와, 배열의 요소가 정해지면 다음 상태를 계산합니다. .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[] 🔒 ES3

    index가 startend 사이의 요소들인 새로운 배열을 반환합니다.

    > ['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 🔒 ES5

    callback이 적어도 하나의 요소에 대해 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' ]
    
  • splice(start: number, deleteCount=this.length-start, ...items: T[]): T[] ✏️ ES3

    start 인덱스부터 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' ]
    

More information

Sources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment