how to compress lines of code with jquery?
<p>I have the following lines of code that I am using.  But it just seems redunent and I was curious if there is an optimized alternative to the following...</p>

<pre><code>if(mkey[65]){ //this is left! (a)
    var nextpos = $("#item").x()-player.speed;
    if(nextpos &gt; 0){
        $("#item").x(nextpos);
    }
}
if(mkey[68]){ //this is right! (d)
    var nextpos = $("#item").x()+player.speed;
    if(nextpos &lt; pg.width - 100){
        $("#item").x(nextpos);
    }
}
if(mkey[87]){ //this is up! (w)
    var nextpos = $("#item").y()-player.speed;
    if(nextpos &gt; 0){
        $("#item").y(nextpos);
    }
}
if(mkey[83]){ //this is down! (s)
    var nextpos = $("#item").y()+player.speed;
    if(nextpos &lt; pg.height - 30){
        $("#item").y(nextpos);
    }
}
</code></pre>

<p>I thought about using the jquery each method, but it only took me so far, because I don't know if you can store custom JavaScript functions into data objects...</p>

<p>Thanks for any advice!</p>

<p><strong>This is what I have tried...(no luck)</strong></p>

<pre><code>$.each([
    {keypress: mkey[65], item:$("#item").x()-player.speed},
    {keypress: mkey[68], item:$("#item").x()+player.speed},
    {keypress: mkey[87], item:$("#item").y()-player.speed},
    {keypress: mkey[83], item:$("#item").y()+player.speed}
], function(i, obj) {

     if (obj.keypress) {
  if(obj.item &gt; 0) { $("#item").x(obj.item);}
  }

});
</code></pre>
