[javascript] 객체의 프로퍼티를 찾는 방법

자바스크립트에서 객체의 프로퍼티를 찾는 방법은 다양합니다. 이 글에서는 객체 프로퍼티를 찾는 여러 가지 방법에 대해 알아보겠습니다.

1. 점 표기법 (Dot notation)

가장 일반적인 방법은 점 표기법을 사용하여 객체의 프로퍼티에 접근하는 것입니다.

const person = {
  name: 'Alice',
  age: 30
};

console.log(person.name); // 'Alice'
console.log(person.age); // 30

2. 대괄호 표기법 (Bracket notation)

대괄호 표기법을 사용하여 객체의 프로퍼티에 접근하는 것도 가능합니다.

const person = {
  name: 'Alice',
  age: 30
};

console.log(person['name']); // 'Alice'
console.log(person['age']); // 30

3. hasOwnProperty 메서드 활용

객체의 hasOwnProperty 메서드를 사용하여 프로퍼티의 존재 여부를 확인할 수 있습니다.

const person = {
  name: 'Alice',
  age: 30
};

console.log(person.hasOwnProperty('name')); // true
console.log(person.hasOwnProperty('job')); // false

4. Object.keys 메서드 활용

Object.keys 메서드를 사용하여 객체의 프로퍼티를 배열로 반환할 수 있습니다.

const person = {
  name: 'Alice',
  age: 30
};

console.log(Object.keys(person)); // ['name', 'age']

5. for…in 루프 활용

for...in 루프를 사용하여 객체의 프로퍼티를 순회할 수 있습니다.

const person = {
  name: 'Alice',
  age: 30
};

for (let key in person) {
  console.log(key + ': ' + person[key]);
}

이렇게 다양한 방법으로 객체의 프로퍼티를 찾을 수 있습니다.

참고 자료: MDN 웹 문서