0

I have this JavaScript object

Object {VoteTag: Object, ResortVoteTag: Array[1]}
  ResortVoteTag: Array[1]
   0: Object
   id: "1"
   resort_id: "1561"
   tag_id: "4"
   user_id: "31"
   vote: "3"
   length: 1
 VoteTag: Object
   id: "4"
   tag: "Snowboarders"
   type: "slopes"

I'm trying to access the vote property in the ResortVoteTag array however I am unable to do so.

This is the following code I'm using.

$.ajax({
                url: "/Votetags/alltags",
                async: false,
                dataType: 'json', 
            }).done(function ( data ) {
                for(var i = 0; i < data.length; i++){
                        console.log(data[i].ResortVoteTag.id);
                }
            });
3
  • Probably want data[i].ResortVoteTag[j].id (where j is another loop iterator). Your description of the object includes two Array definitions. (though i have to say that way of describing an object is confusing...) Commented Aug 24, 2013 at 20:45
  • 2
    Your browser has a fully-featured debugger built into it. Set a breakpoint on the first line of your done function and look at the data you're receiving. Commented Aug 24, 2013 at 20:46
  • T.J. is right, or you just console.log(data) and look at it in console. Commented Aug 24, 2013 at 20:47

1 Answer 1

2

data is an object, based on your example data, and ResortVoteTag is the array. So you need to iterate over that specifically.

for(var i = 0; i < data.ResortVoteTag.length; i++){
    console.log(data.ResortVoteTag[i].id);
}
Sign up to request clarification or add additional context in comments.

Comments

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.