efficient way to search an associative, multi-dimensional array (object) using JavaScript and/or jQuery for key based on value

javascript, jquery, object, search

Solution

Other than changing your data structure like chris suggests this is pretty much your only option:

function getKey(obj, prop, val) {
    var keys = [];

    for (var key in obj) {
        if (obj[key].hasOwnProperty(prop) && obj[key][prop] === val) {
            keys.push(key);                
        }            
    }

    return keys;
}

Nested loops are not required and you only go over each array element once.. pretty efficient in my opinion.

Problem

I have an object that looks similar to this: ``` var arr = {}; arr.planes = { prop1 : 'a', prop2 : 'b', prop3 : 'c' }; arr.trains = { prop1 : 'x', prop2 : 'y', prop3 : 'z' }; arr.autos = { prop1 : 'red', prop2 : 'orange', prop3 : 'blue' }; ``` I am trying to write a function (that is fast and efficient) that will return the key (or array of keys if there are more than 1) of the outer-most array based on a key/value pair nested within. Something like: ``` function getKey(obj, prop, val) { // do some stuff... return key; } var myKey = getKey(arr, 'prop2', 'orange'); ``` The value of myKey should be "autos". I'm pretty sure this can be done with a couple of nested for loops but these arrays are rather large and I'm thinking that, especially using jquery's grep(), there has to be a better way... or maybe not - I'm just stumped at the moment. Any insight would be greatly appreciated!!

Original source