Skip to content

[문제] js의 array가 객체라서 갖는 특징이 뭘까요?  #12

Description

@live-small

질문

자바스크립트의 배열은 인덱스를 키로 갖는 객체입니다.
그렇기에 갖는 특징은 뭐가 있을까요?

상세내용

(제가 생각한 것)

  1. 다른 언어와 달리, 한 배열에 다양한 타입의 원소를 할당할 수 있습니다.
    -> 이렇게 유연한 게 이점으로 작용하는 상황이 있을까요?

  2. 정수형 문자열로 해당 위치의 원소에 접근이 가능합니다.
    -> 이게 과연 좋은걸까요? 저는 아니라고 생각하는데요, 그 이유는 참고에 적어놓을게요.

참고

const arr = [1, 2, 3, 4, 5];

for (let elem in arr) {
    console.log(elem);
    // 0,1,2,3,4 - for in을 이용하면, 배열의 인덱스를 반환합니다. 
    // string 타입으로 반환합니다. 객체의 키는 string만 가능하니까요. 
}

arr.map((value, index) => index);
// 고차함수의 index인자는 number타입입니다. 

정리하자면,
배열에 접근할 때 인덱스를 number, string 둘 다 쓸 수 있습니다.
인덱스를 받는 경우엔, 호출방법에 따라 다른 타입을 줍니다.

  • for in문 : string
  • 콜백함수 : number

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions