开发者

Reading out a JS object

I have been messing around for a project I'm working on with arrays in JS. However, since this didn't work out, I had to turn to objects. Never having used these, I'm wondering about something fairly simple, yet complicated to me. I have the following code:

var ticket_amount = {};
var days = $(".t_days_" + ticket_id).val().split(',');

for(var i = 0; i < days.length; i++)
{
    if (! ticket_amount[days[i]]) 
    {
        ticket_amount[days[i]] = 0;
    };
        ticket_amount[days[i]] += num_tickets;
}

This gives me my output as follows:

Reading out a JS object

I now want to use the information in this object to display some more information. More specifically, I need to get both the date and the ticketnumber out so I can work with them in jQuery. I'm not sure how to do this, though.

I've tried stuff like:

for(tickets in ticket_开发者_如何转开发amount) { }, for(var i = 0; i < ticket_amount.length; i++) {}, but none of these options seem to work. How do I get the information out in this specific case? Thanks a lot.

I won't be on the computer after posting this so I won't be able to answer to any questions for now, but I will find time for it tomorrow. Thanks in advance.


You were almost correct. This would print the data you need, for example.

for(ticket in ticket_amount)
{
    console.log("Ticket:" + ticket + " amount: " + ticket_amount[ticket]);
}

EDIT:

of course, ticket in the above example should have been named just a tad better :)


for(day in ticket_amount){
    // Here day contains the day, and ticket_amount[day] contains the number of tickets
    alert(day+': '+ticket_amount[day]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜