판다꼬마 2022. 5. 15. 22:38
728x90
const personKeys = Object.keys(person);


let person = {
  name: "추성준",
  age: 24,
  tall: 177
};


const personKeys = Object.keys(person);

for (let i = 0; i < personKeys.length; i++) {
  const curKey = personKeys[i];
  const curValue = person[curKey];

  console.log(`${curKey}: ${curValue}`);
}

Object.key의 의미는
이 객체의 키들을 배열로 반환해서 돌려준다

이렇게 전체 코드를 실행해보면
객체를 배열처럼 순회를 할 수 있다.



const personvalues = Object.values(person);

for (let i = 0; i < personvalues.length; i++) {
  console.log(personvalues[i]);
}


벨류 값만 가지고 오고 싶으면 이렇게 하면 된다.

728x90