Question: I have the following simple jquery snippet

```
        $(document).ready(function () {
        $.ajax({
             url:"myjson.json",
             dataType: 'json',
             success:function(json){
                 $.each(json, function() {
                    alert("test");
                });
             },
             error:function(){
             },
        });
    });

```

The result can return 1000's of results - i only want the first 20 for example. How would i best do this?

Answer: 
The callback for `.each()` has an argument `index` which is the index in the original collection. You break out of the iteration by returning false from the callback.

So you would do something like:

```
$.each(json, function(index) {
    if(index < 20)
      alert("test");
    else
       return false;
});

```

As others has commented it is better to do this server side, but maybe you have no access to server side code?
In case you really want to (or have to) do this client side you can break an `$.each` loop by returning `false` at a certain iteration.

See **[this fiddle](http://jsfiddle.net/92pDy/)** for a basic example of how to do this.

In your case that would lead to:

```
success:function(json){
                 $.each(json, function(index) {
                    alert("test");
                    if (index === 19){return false}
                });
             }

```

Also see **[the docs](http://api.jquery.com/jQuery.each/)**:

> 
> We can break the $.each() loop at a particular iteration by making the
>  callback function return false. Returning non-false is the same as a
>  continue statement in a for loop; it will skip immediately to the next
>  iteration.
> 
> 
>