1

I am passing a set of options as an object:

var options={
    sortRules:[
        {...}, // rule 1
        {...}, // rule 2
        // etc.
    ],
    filterRules:[
        {...}, // rule 1
        {...}, // rule 2
        // etc.
    ],
    etc.
};

My issue: if any rule contains an "encodedName" property, I need to retrieve ASAP a dictionary of codes via a Web service. The dictionary is not needed if no "encodedName" property gets passed.

What is the most efficient way to check if an "encodedName" property is present within this object hierarchy? Looping through all the objects and sub-objects sounds painful, and I was wondering if there is a faster way.

[edit]: maybe I should have mentioned another idea I had: use JSON.stringify to turn the object into a string, then use indexOf search for "encodedName". I am not sure if it's more efficient than sub-object iteration though.

Christophe
  • 27,383
  • 28
  • 97
  • 140
  • Could you require the caller to include a `hasEncodedName: true` option? Other than that, I don't think there's anything less painful than searching through the object. – Barmar Sep 29 '13 at 22:18
  • @Barmar I'd rather not, as I should be able to get this information programmatically. – Christophe Sep 29 '13 at 22:25
  • 1
    Your `stringify` idea will probably work, but what if the application happens to have a string in the data that matches that? It's a really crude method. – Barmar Sep 29 '13 at 22:29
  • How many rules will there be? Why do you consider iterating as painful? From a performance perspective it probably won't make a difference. – Felix Kling Sep 29 '13 at 22:35
  • @Barmar agree with that, I want clean code, although getting encodedName under quotes somewhere else is highly unlikely! If it is the case, I might still accept the risk, as the worst that can happen is that the dictionary will be retrieved but useless. – Christophe Sep 29 '13 at 22:38
  • @FelixKling there could be maybe a dozen rules, buried at different levels, along with another dozen options that are not rules (but would be mixed with the rules). – Christophe Sep 29 '13 at 22:40

2 Answers2

0

I would consider using two types of rules - one with and one without the encodedName property. That way you can infer from the category if the rule contains the property in question or not.

Alternatively I would use a recursive function to iterate over all properties to locate the property you're looking for

TGH
  • 38,769
  • 12
  • 102
  • 135
  • Interesting advice (two types of rules). Unfortunately it makes everything else complicated (when later I need to iterate through the rules). – Christophe Sep 29 '13 at 22:09
-1

you could iterate over the options object and then check each property (sortRules, filterRules) if it has the property or not.

for(var rules in options){
    if(options[rules].hasOwnProperty("encodedName"){...}
}

should do it.

Just found that each rules property is an array, in this case there is no other way then iterating through it.

MichaC
  • 13,104
  • 2
  • 44
  • 56
  • It's actually more complicated than that. The example in my question shows 3 levels, and in some cases I might even have 4. Your answer is missing one level. – Christophe Sep 29 '13 at 22:06
  • @Christophe yep just recognized that you have an array of rules in each rules property... I think you would have to iterate through everything ;) – MichaC Sep 29 '13 at 22:07
  • right, it's arrays of rules (and there was a typo in my example, sorry). – Christophe Sep 29 '13 at 22:10