访问复杂的js对象

时间:2020-04-17 16:30:46

标签: javascript json object

我有一个从API端点接收到的以下JSON对象,如何访问该对象的description属性-“ description”:“ Dorian Market 1”?

let markets = { 
  "result":[
     {
      "townId" : "MEBD",
      "storeId" : "1",
      "address" : "Presernova 1",
      "description" : "Dorian Market 1",
      "minOrderValue" : "500",
      "notes" : ""
   } 
      ,
        {
      "townId" : "MEBD",
      "storeId" : "1",
      "address" : "Presernova 2",
      "description" : "Dorian Market 2 ",
      "minOrderValue" : "500",
      "notes" : ""
   } 
       ]
}

这是正确的方法吗? market.result [0] .description,这是正确的方法吗?

1 个答案:

答案 0 :(得分:1)

是的,访问description数组第一个元素的result属性的正确方法是使用markets.result[0].description

let markets = {
  "result":[
    {
      "townId" : "MEBD",
      "storeId" : "1",
      "address" : "Presernova 1",
      "description" : "Dorian Market 1",
      "minOrderValue" : "500",
      "notes" : ""
    },
    {
      "townId" : "MEBD",
      "storeId" : "1",
      "address" : "Presernova 2",
      "description" : "Dorian Market 2 ",
      "minOrderValue" : "500",
      "notes" : ""
    }
  ]
};

console.log(markets.result[0].description)