1020 TIL

기멜·2021년 10월 20일
0

자바스크립트 독학

목록 보기
38/44

객체 모르는 부분...!

Assignment

  • accessObject 함수를 작성해주세요.
  • myStorage 객체의 속성에 접근하여 glove box 프로퍼티의 값을 변수 gloveBoxContents에 대입해주세요.
  • 함수의 리턴값은 glove box 프로퍼티의 값이 되어야 합니다.
function accessObject() {
  // 아래의 코드를 수정하지마세요.
  let myStorage = {
    "car": { 
      "inside": {
        "glove box": "maps",
        "passenger seat": "crumbs"
       },
      "outside": {
        "trunk": "jack"
      }
    }
  };
  
  // 아래의 코드를 수정해주세요.
  let gloveBoxContents = 
  myStorage['car']['inside']['glove box'];
  // gloveBoxContents["glove box"]
  
  
  // 아래의 코드를 수정하지마세요.
  return gloveBoxContents;
}

이해가 잘 안갔는데 스터디원께서 이렇게 알려주셨다.

(1) 대괄호 표기법

let gloveBoxContents = 

  myStorage['car']['inside']['glove box'];



(2) 대괄호 표기법 + Object.values(obj)

let obj = myStorage['car']['inside'];

//[object Object] {
  //glove box: "maps",
  //passenger seat: "crumbs"
//}


let gloveBoxContents = Object.values(myStroge)[0]; // value 값만 가져옴

=> ["maps", "crumbs"]

Object.values(obj);

Object.keys(obj);

Object.entries(obj); => 키, 밸류 둘다 구하는

{ -> 0 index
      type: "flowers",
      list: [
        "rose",
        "tulip",
        "dandelion"
      ]
    } -> a
================
    { -> 1 index
      type: "trees",
      list: [
        "fir",
        "pine",
        "birch"
      ]
    } -> b

 => arr [a, b]
arr[1] === b

myPlants[1] => object./[]
 { 
      type: "trees",
      list: [
        "fir",
        "pine",
        "birch"
      ]
    } 

.list -> myPlants[1].list = [ "fir", "pine", "birch"  ]

myPlants[1].list[1] => "pine"

one step
profile
프론트엔드 개발자를 꿈꾸는 도화지 위를 달리는 여자

0개의 댓글