tags:

views:

655

answers:

1

Hi, I have a JSon array as below which I need to filter to get the correct child values from the test data below.

var arrChildOptions2 = [{Parent:'opt1',Value:'opt1',Text:'Parent1 - Child 1'},{Parent:'opt2',Value:'opt1',Text:'Parent 2 - Child 1'},{Parent:'opt2',Value:'opt2',Text:'Parent 2 - Child 2'}];

The values are used to populate a dropdown based on the change event of a parent dropdown as below.

    $(function() {
     $('#ddl1').change(function() {
             $('#ddl2 option:gt(0)').remove();
             $('#ddl2').addItems('#ddl2', arrChildOptions2[Parent=opt2]);
     });
});

where additems is a function that loops through the array. Problem is I can't get it to filter by parent, I've tried using contains and the above arrChildOptions2[Parent=opt2] but I can't get it to filter, I'd prefer to find a neat solution rather than use a for loop? Any ideas, cheers

+1  A: 

You might have more luck using the jQuery.grep() function rather than messing around with loops.

This function "Finds the elements of an array which satisfy a filter function. The original array is not affected".

Phil.Wheeler
Excellent Phil, thank you, I wasnt aware of that function, still pretty new to jquery, works a treat.
Israfel