我有点挣扎于$.grep和比较数组。这是一个具体的问题,所以我会尽量保持简单。
我有一个列有商店列表的数组,其中包含额外的列表。例如厕所、无线上网和咖啡。厕所:"1",wifi:"0",咖啡:"0“
我有输入字段(复选框)。
var extras = [];
$(".input-extra").on("change", function(e){
if(this.checked){
extras.push(this.name);
}else{
extras.remove(this.name);
}
});如果我选中一个输入复选框字段,我的Array将被填充输入字段的名称(例如“马桶”)。
现在我想展示符合我的输入字段的商店。(对不起,我不知道如何更好地描述它,所以我张贴了一些例子)
例如,shop1提供咖啡和无线网络。我在找有无线网络的商店。因此,商店1将被展示。
shop2提供咖啡和厕所。我在找有无线网络的商店。商店2将不会显示。这很好:但是我如何嵌入额外的内容呢?
function fillShopListFilter(data, zip, extras, pricemin, pricemax, squaremetermin, squaremetermax) {
var length = $.grep(shortShopList.shops, function( n, i ) {
return zip.includes(parseInt(n.zip)) && parseInt(n.price)>=pricemin && parseInt(n.price)<=pricemax && parseInt(n.squaremeter)>=squaremetermin && parseInt(n.squaremeter)<=squaremetermax;
});
.....
}我想这很清楚。
谢谢你的回答。
发布于 2018-07-29 09:12:14
我自己回答的。
var length = $.grep(shortShopList.shops, function( n, i ) {
return zip.includes(parseInt(n.zip)) && parseInt(n.price)>=pricemin && parseInt(n.price)<=pricemax && parseInt(n.squaremeter)>=squaremetermin && parseInt(n.squaremeter)<=squaremetermax;
});
var newlength = $.grep(length, function( n, i ) {
var x = 0;
$.each(n.extras, function (indexInArray, element) {
if(extras.includes(indexInArray) && parseInt(element) == 1){
x++;
}
});
return x == extras.length;
});发布于 2018-07-29 07:50:36
JQuery grep:
查找满足筛选函数的数组元素。原始数组不受影响。
var extras = [];
$(".input-extra").on("change", function(e) {
if (this.checked) {
extras.push(this.name);
} else {
extras = $.grep(extras, function(extra) {
return extra != this.name;
});
}
});https://stackoverflow.com/questions/51578286
复制相似问题