简体   繁体   English

从特定的javascript对象属性获取价值

[英]Get value from specific javascript object attribute

I'm trying to access the locations array from my javascript object and store it. 我正在尝试从我的JavaScript对象访问Locations数组并将其存储。

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

I've tried things like: 我已经尝试过类似的事情:

locations = data['locations'] and data.locations . locations = data['locations']data.locations But both return undefined . 但是两者都返回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! (不使用Jquery)感谢您的帮助!

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. 如果从data声明中删除[] ,则可以避免[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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