views:

69

answers:

2

I have been doing tons of Ajax recently and work with lots of my domain objects converted their JavaScript counterparts. I found myself writing several functions that looped through the arrays of objects to search by property values. I decided to write a simple jQuery plugin that would allow me to search an array of primitive or complex types.

Note: This is my first attempt at writing a jQuery plugin. I'm not sure if i went about it the correct way. My main concern is efficiency and cross-browser support of the search logic.

Thanks!

Code to critique:

$(function() {
    $.indexOfArray = function (search, array) {
        var keys = [];
        var index = -1;
        var primitiveType = true;
        for (var propertyName in search) {
            primitiveType = false;
            keys.push(propertyName);
        }

        if ($.isArray(array)) {
            for (var a = 0; a < array.length; a++) {
                var match = 0;
                if (primitiveType && array[a] == search) {
                    index = a;
                    break;
                }
                else if (!primitiveType) {
                    for (var b = 0; b < keys.length; b++) {
                        if (search[keys[b]] == array[a][keys[b]]) {
                            match++;
                        }
                    }
                    if (match == keys.length) {
                        index = a;
                        break;
                    }
                }

            }
        }
        return index;
    }
});

Simple example of use (It also works with primitive type arrays such as strings or ints):

var testArray = [{Id:1, FirstName: 'John'}, {Id:2, FirstName: 'Jake'}, {Id:2, FirstName: 'Jason'}];

var selectedPerson = testArray[$.indexOfArray({Id:2}, testArray)];
+1  A: 

As far as cross browser goes, you don't really have anything to worry about.

In terms of efficiency, it's not bad, though you could probably do with caching some values, for examples you lookup the .length property of the arrays a lot. Also with keys[b].

Evan Trimboli
1+ Good call, thanks!
used2could
+1  A: 

Really nice plugin. Post it up on github while you're at it. A couple of things:

Don't call your function indexOfArray because it's not doing finding the index of a given object, but rather doing searches against keys and values given a partial/full object as a template. It also searches on primitives but that's more of an added bonus. You don't have to go with past conventions over naming this thing. Use whatever name describes it best.

You could do without declaring the keys array, and space on some space there. But that's a micro-optimization unless you're dealing with very large objects.

This may just be wishful thinking, but a search with nested object structure as search key would be really awesome :)

Nice plugin and code. Would be super useful in querying JSON documents.

Anurag
Thanks. I didn't think about putting it on gitHub but i'll do that.
used2could