[JS] 객체

soor.dev·2021년 3월 9일
0

Java Script

목록 보기
23/26
post-thumbnail

객체let obj = {key1:value, key2:value2}로 나타나며, 배열처럼 순서에 상관없이 원하는 key값을 불러올 수 있다. 또한 선언한 변수는 공통적이지만 그 안의 내용이 각기 다를 때, 객체를 사용한다.

let intro = {
  firstName : 'soor',
  lastName : 'Kang',
  email : 'hyunsoosiesta@gmail.com',
  city : 'Jeju city'
};

difference between dot notation and bracket notation

Dot Notation

hs.firstName // 'soor'
hs.city // 'Jeju city'

Bracket Notation (key값이 동적일 경우에 사용)

hs['firstName'] // 'soor'
hs['city'] // 'Jeju city'

let Pudding = {isSweet : true};

For (let i in obj)

object에서 사용 (iterable 하지 않은)

Q. 객체의 모든 key를 콘솔에 출력하기 위한 방법??
  
function allKeys(obj) {
  for(let key in obj) {
    console.log(key);

For (let i of str)

string, array 등 유사배열에서 사용 (iterable 한)

0개의 댓글