提问人:Chihiro Japan 提问时间:5/3/2023 最后编辑:Chihiro Japan 更新时间:5/3/2023 访问量:22
jquery 中通过选择选项框筛选对象数组
FIlter object array by select option box in jquery
问:
我有 2 个选择选项框,使用这些选定的值,我想从对象数组数据中查找数据。
我可以像这样在数组中获取值数据
['black','7']
而数组数据想要过滤就是这样。
array[0:{id:01,name: "Glove black size7" , attributes:{d001:value:black, d002:value:7}},
1:{id:01,name: "Glove pink size7" , attributes:{d001:value:pink, d002:value:7}]
//in jquery page I run onChange function
$(document).on('change', '.variant select', function(a) {
a.preventDefault();
var select = $(this).val();
var attribute = a.target.name;
var selectedVals = $('select').map(function() { return this.value; }).get();
changeAttribute(select, attribute, selectedVal);
})
function changeAttribute(select, attribute, selectedVal) {
//Get data from element and get loop array data which I want to filter
var variant = JSON.parse($('.variant').attr('data-value'));
for (var i = 1; i < variant.length; i++) {
// Inside here I want to filter object array data and find a data
}
<div class="variant">
<select name="d001" id="attribute">
<option value="black">Black</option>
<option value="pink">Pink</option>
<option value="white">White</option>
</select>
<select name="d002" id="attribute">
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</div>
答: 暂无答案
评论
data-value
$("option:selected").val()
$(this).val()
changeAttribute