2

I have a site made as shown in this jsfiddle:

http://jsfiddle.net/bAz3y/

$.getJSON('eventbrite.json', function(data) {
    var events = [];
    var j = 0;

    for (var i in data) {
        events[i][0] = data[i].event.title;
    }

    $('.btn-lg').click(function() {
        $('#tester').replaceWith("<div id='tester'>" + events[j][0] + "</div>");
        j = j + 1;
    })
    $("div.swiper").on("swipe",function() {
        $('#tester').replaceWith("<div id='tester'>" + events[j][0] + "</div>");
        j = j + 1;
    })
});

I'm trying to get the 2nd dimension to store the event title, so that I can then add event date and distance in events[j][1] and events[j][2].

However, adding the second dimension (ie the events[i][0]) means no results are returned.

1 Answer 1

1

The problem is the for loop, events[i] is undefined so events[i][0] will fail

1.Solution is

for (var i in data) {
    events[i] = [data[i].event.title, data[i].event.some];
}

2.Another is to use $.each() and do

var events = [];
var j = 0;

$.each(data, function(_,item){
    events.push([item.event.title, data[i].event.some])
});

3.A third way is to use $.map()

var j = 0;

var events = $.map(data, function (item) {
    return [[item.event.title, data[i].event.some]]
});
Sign up to request clarification or add additional context in comments.

2 Comments

Using the top suggestion, events[i] works, but using events[i][0] does not.
@user3057706 it is creating a two dimensional array.. after the for loop try events[0][0] not inside

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.