简体   繁体   中英

Get value from specific javascript object attribute

I'm trying to access the locations array from my javascript object and store it.

data = [{
          "id":123,
          "name":"John",
          "locations":["Smith","McHale","Residence 4"]
        }]

I've tried things like:

locations = data['locations'] and data.locations . But both return undefined . What am I doing wrong? How can I just grab the locations array to store in its own variable? (Not using Jquery) Thank you for your help advance!

Because it's array first, and only then object:

locations = data[0].locations;

You could avoid [0] this if you remove [ and ] from your data declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM