dtjmsy
dtjmsy

Reputation: 2762

Jquery function with 2 arguments

I am reading this very interesting article about knockoutJs ( http://wildermuth.com/2011/11/20/Using_MVVM_on_the_Web_with_KnockoutJS )

And I came across this jquery call:

        $.each(response.results, function (x, game) {
          theViewModel.games.push(new gameModel()
          .id(game.Id)
          .name(game.Name)
          .releaseDate(game.ReleaseDate)
          .price(game.Price)
          .imageUrl(game.ImageUrl)
          .genre(game.Genre));
        });

what I don' t understand is the function (x, game), the x is the index isn' t ?, how about the 'game' argument, where is it coming from ?

Upvotes: 0

Views: 82

Answers (1)

Fenton
Fenton

Reputation: 251292

The callback you supply to $.each will be called once for eaach result in response.results. When it calls the callback it will pas in the index and the result.

For example, it does this (illustrative example)

for (var i = 0; i < response.results.length; i++) {
    yourCallback(i, response.results[i]);
}

Upvotes: 1

Related Questions