使用jQuery过滤JavaScript Array中的项目

Isr*_*fel 13 javascript jquery

我有一个JavaScript数组如下所示,我需要过滤以从下面的测试数据中获取正确的子值.

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'}
    ];
Run Code Online (Sandbox Code Playgroud)

这些值用于根据父下拉列表的更改事件填充下拉列表,如下所示.

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

其中additems是循环遍历数组的函数.问题是我无法通过父进行过滤,我尝试过使用contains和上面的arrChildOptions2 [Parent = opt2]但是我无法过滤它,我更愿意找到一个整洁的解决方案而不是使用一个for循环?任何想法,欢呼

Phi*_*ler 30

使用jQuery.grep()函数可能会有更多的运气,而不是用循环.

此函数"查找满足过滤函数的数组元素.原始数组不受影响".