How to find an object in an array of objects if some array elements are undefined?
<p>I have an array of objects with the following format:</p>

<pre><code>obj = { ref: 8, id: "obj-8" }
</code></pre>

<p>and a function which uses jQuery's <em>grep</em> method to return an item from that array, by searching for the object <em>ref</em> property:</p>

<pre><code>function returnObj(arr,r){
    return $.grep(arr, function(elem,index){ return elem.ref == r; })[0];
}
</code></pre>

<p>If I use this function on an array that has undefined elements in it (they were previously deleted using the <em>delete</em> operator), I get the following error: <em>Uncaught TypeError: Cannot read property 'ref' of undefined</em>, which I assume is thrown when an undefined element is encountered.</p>

<p>How can I modify the function so it doesn't break?</p>
