-2

I have the following object called data, how can I access the value for key name using Javascript

const data = {
    "bar2": {
        "address": "138/140 Gouger St, Adelaide SA 5000",
        "appStoreURL": "http://itunes.apple.com/app/idXXXXXXXXX",
        "description": "Disco Mexico, blends party vibes with fuss-free Mexican food in a small-bar setting.",
        "imgURLs": [
            "https://url",
            "https://url"
        ],
        "lat": -34.848082,
        "lon": 138.599813,
        "name": "Disco Mexico Taqueria",
        "phone": "0416 855 108",
        "status": "active",
        "venueImgURL": "https://firebasestorage.googleapis.com/v0/b/vipeeps-2018.appspot.com/o/venueImages%2FDisco_Mex_o.jpg?alt=media&token=60d76240-221c-415c-8d0d-7324d95a30ba"
    }
}
mallikarjun
  • 1,862
  • 5
  • 23
  • 47
Roggie
  • 1,157
  • 3
  • 16
  • 40

3 Answers3

0

const data = {
  "bar2": {
    "address": "138/140 Gouger St, Adelaide SA 5000",
    "appStoreURL": "http://itunes.apple.com/app/idXXXXXXXXX",
    "description": "Disco Mexico, blends party vibes with fuss-free Mexican food in a small-bar setting.",
    "imgURLs": [
      "https://url",
      "https://url"
    ],
    "lat": -34.848082,
    "lon": 138.599813,
    "name": "Disco Mexico Taqueria",
    "phone": "0416 855 108",
    "status": "active",
    "venueImgURL": "https://firebasestorage.googleapis.com/v0/b/vipeeps-2018.appspot.com/o/venueImages%2FDisco_Mex_o.jpg?alt=media&token=60d76240-221c-415c-8d0d-7324d95a30ba"
  }
}
console.log(data.bar2.name)
holydragon
  • 6,158
  • 6
  • 39
  • 62
  • Please note, if a post is this basic, its most likely that it has been answered before. You should link post to that post instead of answering. If you still feel, that post does not answer your approach, you may answer but then please add explanation as to what/why you have changed and how it works – Rajesh Feb 27 '19 at 06:22
0

Access the property how you would any other - data.bar2.name:

const data = {
  "bar2": {
    "address": "138/140 Gouger St, Adelaide SA 5000",
    "appStoreURL": "http://itunes.apple.com/app/idXXXXXXXXX",
    "description": "Disco Mexico, blends party vibes with fuss-free Mexican food in a small-bar setting.",
    "imgURLs": [
      "https://url",
      "https://url"
    ],
    "lat": -34.848082,
    "lon": 138.599813,
    "name": "Disco Mexico Taqueria",
    "phone": "0416 855 108",
    "status": "active",
    "venueImgURL": "https://firebasestorage.googleapis.com/v0/b/vipeeps-2018.appspot.com/o/venueImages%2FDisco_Mex_o.jpg?alt=media&token=60d76240-221c-415c-8d0d-7324d95a30ba"
  }
};
const name = data.bar2.name;
console.lo(name);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 3
    Should you not mark the post as dupe? – Rajesh Feb 27 '19 at 06:20
  • @Rajesh Sorry I couldn't find the post it was a duplicate of. Also, I don't have a gold tag badge, so I couldn't on my own. – Jack Bashford Feb 27 '19 at 06:21
  • Its not about badges. You should look for a similar post and share its link in comments. A user with necessary privilege would close it. Objective is to reduce duplicate content. – Rajesh Feb 27 '19 at 06:23
  • Yes @Rajesh, but I was looking for the dupe link when you closed the question. – Jack Bashford Feb 27 '19 at 06:24
  • Also, no need to apologies to anyone. If users are offended or think your answer is not required, they would downvote. So no apologies required, unless you behave rudely. – Rajesh Feb 27 '19 at 06:25
0

You can use . notation like data.bar2.name, or index notation like data['bar2']['name']

Nafees Anwar
  • 6,324
  • 2
  • 23
  • 42