배열

  • 참조사이트 : mdn guide
  • 배열은 이름과 인덱스로 참조되는 정렬된 값들의 집합
  • Indexed collections
    1. 자바스트립트의 배열은 배열에 포함된 요소들을 배열의 인덱스 값을 속성 이름으로 사용하여 표준 객체의 속성처럼 저장
  • Keyed collections
    1. Object
    2. Map
    3. Set
  • object와 map이 비교
    1. Object의 키는 Strings이며, Map의 키는 모든 값을 가질 수 있다.
    2. Object는 크기를 수동으로 추적해야하지만, Map은 크기를 쉽게 얻을 수 있다.
    3. Map은 삽입된 순서대로 반복된다.
    4. 객체(Object)에는 prototype이 있어 Map에 기본 키들이 있다. (이것은 map = Object.create(null) 를 사용하여 우회할 수 있다. )
    5. 실행 시까지 키를 알수 없고, 모든 키가 동일한 type이며 모든 값들이 동일한 type일 경우에는 objects를 대신해서 map을 사용해라.
    6. 각 개별 요소에 대해 적용해야 하는 로직이 있을 경우에는 objects를 사용해라.

배열 속성과 메서드

  • length를 이용한 배열 길이 변경

    1. arr.length = 5 : 배열의 길이를 늘림
    2. arr.length = 0 : 배열 초기화
  • splice : 배열 요소들의 바른 추가, 변경, 삭제

    1. arr.splice(시작인덱스, 삭제할요소길이, 추가할요소1, 추가할요소2..)
  • 배열의 맨앞에 요소 추가 삭제(stack)

    1. unshift = splice(0,0,’new’)
    2. shift
  • 배열의 맨 뒤에 요소 추가 삭제(queue)

    1. push = splice(arr.length,0,’new’)
    2. pop
  • 배열 요소 삭제 오류

arr1.forEach(function (doll, idx) {
  arr1.splice(idx, 1);
});
  • 누적합 구하기

    1. 배열.reduce((누적값, 현잿값, 인덱스, 요소) => { return 결과 }, 초깃값) //going left-to-right
    2. 배열.reduceRight((누적값, 현잿값, 인덱스, 요소) => { return 결과 }, 초깃값) //going right-to-left
  • 배열 검색

    1. indexOf(검색값, 시작인덱스) // Search the array for an element and returns its position
    2. lastIndexOf(검색값, 시작인덱스) // Search the array for an element, starting at the end, and returns its position
    3. findIndex(el => el.age==4) // Returns the index of the first element in an array that pass a test
    4. find(el => el.age==4) // Returns the value of the first element in an array that pass a test
    5. filter(el => el.age==4) // Creates a new array with every element in an array that pass a test
    6. some(el=> el.age<20) // 특정 조건을 만족하는 요소를 만나면 그 즉시 true 리턴
    7. every(el=> el.age>20) // 배열의 모든 요소가 조건을 만족하는지 true/false 리턴. false를 리턴하는 요소를 만나면 그 즉시 리턴
  • 배열요소의 순환

    1. 인스턴스.forEach() // 인스턴스 메서드. Calls a function for each array element. 원배열을 재가공
    2. 인스턴스.map() // 인스턴스 메서드. Creates a new array with the result of calling a function for each array element
    3. Array.from() // static 메서드. 이터러블 객체를 배열로 바꿔주는 기능
    // Array.from : hrml element 반복
    let arr =  Array.from(document.querySelectorAll("input:checked")).map(ele => ({postId:ele.value}) );
  • 배열 정렬

    1. arr.sort((a,b) => 비교값 ) // Sorts the elements of an array
    2. reverse() // Reverses the order of the elements in an array
Method Description
concat() Joins two or more arrays, and returns a copy of the joined arrays
copyWithin() 배열 내에서 지정된 위치로 배열 요소를 복사
entries() 키/값 쌍 배열 반복 개체를 반환
every() 배열의 모든 요소가 테스트를 통과하는지 확인
fill() Fill the elements in an array with a static value
filter() Creates a new array with every element in an array that pass a test
find() Returns the value of the first element in an array that pass a test
from() Creates an array from an object
includes() Check if an array contains the specified element
indexOf() Search the array for an element and returns its position
lastIndexOf() Search the array for an element, starting at the end, and returns its position
findIndex() Returns the index of the first element in an array that pass a test
isArray() Checks whether an object is an array
join() Joins all elements of an array into a string
keys() Returns a Array Iteration Object, containing the keys of the original array
some() Checks if any of the elements in an array pass a test
sort() 배열 정렬
splice() 배열 요소 추가/삭제/변경
reduce() 누적합 구하기
toString() Converts an array to a string, and returns the result
valueOf() Returns the primitive value of an array

Categories:

Updated: