1 回答
TA贡献1859条经验 获得超6个赞
我用它解决了这个问题$.inArray
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'}
]
console.log(arr);
let yo;
$(`#filter`).on('change', function() {
yo = [];
$('option:selected', this).each(function() {
yo.push(this.value);
});
console.log(yo);
arr.forEach(arr_opts => {
if ($.inArray(arr_opts.name, yo) !== -1) {
arr_opts.in_order = true;
} else {
arr_opts.in_order = false;
}
});
console.log(arr);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id='filter' multiple>
<option value='one' selected>one</option>
<option value='two'>two</option>
<option value='three'>three</option>
<option value='four'>four</option>
</select>
添加回答
举报