0

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!

1
  • As nicael alluded to, you don't need [ ] on an object. Using those brackets will put it in an array. Commented Jun 17, 2016 at 19:25

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You answered so quickly it wouldn't let me. Done! Thanks again :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.