Right...

In this case, how about adding the following

/*Replicate php in_array
--------------------------------------------------------------------------------
*/
function in_array(needle, haystack)
{
        var i = haystack.length;
        if(i > 0)
        {
                do
                {
                        if (haystack[i] === needle) return true;
                }
                while (i--);
        }
        return false;
}


On Jul 31, 11:07 am, Klaus Hartl <[EMAIL PROTECTED]> wrote:
> fambizzari wrote:
> > PHP developers will all know about the array_unique() function which
> > removes duplicate values from an array.
>
> > Up until today, we've been using the following function to replicate
> > this function in Javascript
>
> > function array_unique(arr)
> > {
> >    var newArray = [];
> >    var existingItems = {};
> >    var prefix = String(Math.random() * 9e9);
> >    for (var ii = 0; ii < arr.length; ++ii)
> >    {
> >            if (!existingItems[prefix + arr[ii]])
> >            {
> >                    newArray.push(arr[ii]);
> >                    existingItems[prefix + arr[ii]] = true;
> >            }
> >    }
> >    return newArray;
> > }
>
> > However, i have just come to learn that jquery's javascript utilities
> > provide the same affect using  $.merge().
>
> > So, previously, we would have done arr = array_unique(arr), jquery
> > allows the same thing to be accomplished through $.merge(arr, arr) -
> > the immediate benefit of which is that the above code can be deleted
> > from our core js library.
>
> > 1. Any chance that jquery will support $.unique() as it makes more
> > sense than $.merge(arr, arr)
>
> > 2. Have i missed something?
>
> > 3. Any comments from anyone?
>
> The Array methods plugin[1] also adds a unique method to Arrays:
>
> [1, 2, 1, 4, 5, 4].unique(); // => [1, 2, 4, 5]
>
> The unique method in turn uses the filter method, which is a native
> JavaScript 1.6 method and supported at least by Firefox 1.5 and
> later[2], but I guess Opera and Safari already support JavaScript 1.6 as
> well.
>
> Anyway, the plugin adds all the new JavaScript 1.6 methods in browsers
> that do not support it natively.
>
> [1]http://jqueryjs.googlecode.com/svn/trunk/plugins/methods/array.js
> [2]http://developer.mozilla.org/en/docs/New_in_JavaScript_1.6#Array_extras
>
> --Klaus

Reply via email to