我有一个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'}
];
以下代码将根据父下拉框的更改事件来填充值以用于下拉菜单。
$(function() {
$('#ddl1').change(function() {
$('#ddl2 option:gt(0)').remove();
$('#ddl2').addItems('#ddl2', arrChildOptions2[Parent=opt2]);
});
});
additems是一个循环遍历数组的函数。问题是我无法按父级过滤它,我尝试使用contains和上面的arrChildOptions2[Parent=opt2],但我无法进行过滤。我希望找到一个简洁的解决方案而不是使用for循环。有什么想法,谢谢。
array.filter()
- Blazemonger