我想根据下拉列表中的选择来更改对象数组中的值。
arr= [
{name: 'one', in_order: true, other_key:'ga'},
{name: 'one', in_order: true, other_key:'gb'},
{name: 'one', in_order: true, other_key:'gc'},
{name: 'two', in_order: false, other_key:'gd'},
{name: 'two', in_order: false, other_key:'ge'},
{name: 'two', in_order: false, other_key:'gf'},
{name: 'three', in_order: false, other_key:'gg'},
{name: 'three', in_order: false, other_key:'gh'},
{name: 'three', in_order: false, other_key:'gi'},
{name: 'four', in_order: false, other_key:'gj'},
{name: 'four', in_order: false, other_key:'gk'},
{name: 'four', in_order: false, other_key:'gl'}
]
我这样做了:
$(`#filter`).on('change', function() {
yo = [];
$('option:selected', this).each(function() {
yo.push(this.value);
});
arr.forEach(arr_opts => {
yo.forEach(opt => {
if (arr_opts.name == opt) {
arr_opts.in_order = true;
}
});
});
console.log(arr);
});
我这样做,我得到我的结果。但是当我在下拉列表中取消选择结果时,它不会变回false。例如,如果我检查和.它将数组中两者的 in 值更改为 。但是,如果我随后取消选择 ,则保持为 。onetwotruetwotwotrue
取消选择后,是否可以将所选值更改回 ?false
BIG阳
相关分类