1

I need to access an element with a certain field value for the cdOption field in this array of objects of type possibleOptions:

[Object { cdOption="OPT001", description="Description 1", type="STRING"}, 
Object { cdOption="OPT002", description="Description 2", type="STRING"},
Object { cdOption="OPT003", description="Description 3", type="STRING"}]

The field value I'm looking for is extracted from antoher object in an array and so I'm alreay in a $.each cycle. Can I avoid entering another cycle in order to loop the possibleOptions object and look for the specified field value?

I've tried with possibleOptions[option.cdOpzione] but it doesn't work, is there a way to do this? I know I'm missing something.

current $.each code:

$.each(oldOptions, function(key, option) {    
    $.each(possibleOptions, function(key, possibleOption) {

        if (option.cdOption === possibleOptions.cdOption) {
            console.log(option.cdOption);
            console.log(possibleOption.description);
        }
    });
});
2
  • You seem to have an array of objects, so you need the index of the object you want query first Commented Oct 4, 2013 at 14:27
  • Can you post your current $.each code? Commented Oct 4, 2013 at 14:31

2 Answers 2

3

In a generic way, you can't avoid the extra cycle. There may be particular solutions though, depending on your circumstances.

Solution 1

You could avoid it if you restructure your data, to have possibleOptions be an object with the values in cdOption as keys and an object with description and type as value.

Example:

var possibleOptions = {
  'OPT001' : { description:"Description 1", type:"STRING" },
  'OPT002' : { description:"Description 2", type:"STRING" },
  'OPT003' : { description:"Description 3", type:"STRING" }
};

var val = 'OPT002';
console.log(possibleOptions[val]);

Solution 2

Another thing you could do if the cdOption is always of the form OPT-index- where -index- is 1+ the index in the array is to parse the value you're looking for, extract the -index-, parseInt and subtract one.

Example:

var val = 'OPT002';
var index = parseInt(val.substring(3))-1;
console.log(possibleOptions[index]);

Demo for both: http://jsbin.com/opojozE/1/edit

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. I could use the second solution but I think that in order to be completely sure I'll use the extra cycle. I'm now having 3 for-each cycles, how bad is that?
If there are few options, you're not that much in trouble. The easiest way to do it though is with Array.prototype.filter, mentioned in @Paul Roub's answer. It still loops through the array, but the code is easier to read and as a native function it has more chances of being optimized by the JS engine.
1

Array.filter can return an array of the elements matching a conditon. e.g. if you want to find the object (or objects) with cdOption == "OPT002", you could say:

 var matches = possibleOptions.filter(
    function( element ) {
      return ( "OPT002" == element.cdOption );
    }
 );

and matches will contain:

 [ 
   { cdOption="OPT002", description="Description 2", type="STRING"}
 ]

if you're just looking for one match:

 var myOption = (matches.length > 0) ? matches[0] : null;

If you need to support older browsers that lack Array.filter, see Array filter method at MDN for a way to add it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.