在jQuery中基于两个变量进行搜索 [英] Search based on two variables in jQuery

查看:73
本文介绍了在jQuery中基于两个变量进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据两个变量同时搜索以下数组?



How can I get the following array based on two variables simultaneously search?

var products = [
                     {"id":"1","category":"Apple","price":2860,"image":"images/Apple-iPhone-4.gif","title":"Apple iPhone 4","os":"ios","lsw":"ls"} 
                     , { "id": "2", "category": "BlackBerry", "price": 450, "image": "images/BlackBerry-9780-Bold.gif", "title": "BlackBerry 9780 Bold", "os": "other", "lsw": "ls" }
                     , { "id": "12", "category": "BlackBerry", "price": 520, "image": "images/BlackBerry-Torch-9800.gif", "title": "BlackBerry Torch 9800", "os": "other", "lsw": "ww" }
                     , { "id": "26", "category": "HTC", "price": 1850, "image": "images/HTC-Desire-S.gif", "title": "HTC Desire S", "os": "android", "lsw": "ls" }
                     , { "id": "50", "category": "HTC", "price": 600, "image": "images/HTC-HD7.gif", "title": "HTC HD7", "os": "android", "lsw": "ww" }
                     , { "id": "21", "category": "HTC", "price": 2400, "image": "images/HTC-Incredible-S.gif", "title": "HTC Incredible S", "os": "android", "lsw": "ls" }
                     , { "id": "11", "category": "HTC", "price": 2500, "image": "images/HTC-Sensation.gif", "title": "HTC Sensation", "os": "android", "lsw": "ww" }
                     , { "id": "3", "category": "HTC", "price": 100, "image": "images/HTC-Wildfire.gif", "title": "HTC Wildfire", "os": "android", "lsw": "ww" }
                     , { "id": "6", "category": "Motorola", "price": 2460, "image": "images/Motorola-Atrix-4G.gif", "title": "Motorola Atrix 4G", "os": "ios", "lsw": "ww" }
                     , { "id": "14", "category": "Motorola", "price": 100, "image": "images/Motorola-DEFY.gif", "title": "Motorola DEFY", "os": "ios", "lsw": "ww" }
                     , { "id": "15", "category": "Nokia", "price": 1200, "image": "images/Nokia-C7.gif", "title": "Nokia C7", "os": "ios", "lsw": "ww" }
                     , { "id": "60", "category": "Nokia", "price": 1700, "image": "images/Nokia-N8.gif", "title": "Nokia N8", "os": "ios", "lsw": "ww" }
                     , { "id": "70", "category": "Nokia", "price": 2000, "image": "images/Nokia-X6.gif", "title": "Nokia X6", "os": "ios", "lsw": "ww" }
                     , { "id": "45", "category": "Samsung", "price": 1800, "image": "images/Samsung-I9000-Galaxy-S.gif", "title": "Samsung I9000 Galaxy S", "os": "android", "lsw": "ww" }
                     , { "id": "23", "category": "Samsung", "price": 2900, "image": "images/samsung-I9100-galaxy-S2.gif", "title": "samsung I9100 galaxy S2", "os": "android", "lsw": "ww" }
                     , { "id": "17", "category": "Samsung", "price": 1300, "image": "images/Samsung-S5830-Galaxy-Ace.gif", "title": "Samsung S5830 Galaxy Ace", "os": "android", "lsw": "ww" }
                     , { "id": "99", "category": "Sony-Ericsson", "price": 1400, "image": "images/Sony-Ericsson-XPERIA-X10.gif", "title": "Sony Ericsson XPERIA X10", "os": "android", "lsw": "ww" }
                     ];





但我的方法是添加一个新变量来显示产品。



But my approach is to add a new variable to display the products.

if (!minPrice && !maxPrice) {
                filterdProducts = products;
 
        } else{
            $.each(products, function(i, object) {   
                var curentPrice = parseFloat(object.price); 
                var priceMinOk = true;
                var priceMaxOk = true;
                // filter results match the price range
                if(maxPrice || minPrice){
                    if(maxPrice && maxPrice<curentPrice){
                        priceMaxOk = false;
                    }
                    if(minPrice && minPrice>curentPrice){
                        priceMinOk = false;
                    }
                }  
                //  loop over list and get only related to new array
                if (priceMinOk && priceMaxOk) {
                    a = os.match("android");
                    i = os.match("ios");
                    o = os.match("other");
                    ls = os.match("ls");


                    if (a == "android" || i == "ios" || o == "other" || ls == "ls") {

                        if (object.os == a || object.os == i || object.os == o || object.lsw == ls) {

                            //filterdProducts[key] = object;
                           // key++;
                            var found = $.map(filterdProducts, function (val) {
                                return val.os == "ios", val.lsw == "ls";
                            });


                        }
                    
                    }
                    else {
                        filterdProducts[key] = object;
                        key++;
                    }

                    
                    }
                
                

            });
        }

推荐答案

.each(products, function ( i,object){
var curentPrice = parseFloat (object.price);
var priceMinOk = true ;
var priceMaxOk = true ;
// 过滤结果与价格范围匹配
如果(maxPrice || minPrice){
if (maxPrice&& maxPrice< curentPrice){
priceMaxOk = false ;
}
如果(米inPrice&& minPrice> curentPrice){
priceMinOk = false ;
}
}
// 循环列表并获取仅与新数组相关
if (priceMinOk&& priceMaxOk){
a = os.match( android);
i = os.match( ios);
o = os.match( other);
ls = os.match( ls);


if (a == android || i == ios || o == 其他 || ls == ls){

if (object.os = = a || object.os == i || object.os == o || object.lsw == ls){

// filterdProducts [key] = object;
// key ++;
var found =
.each(products, function(i, object) { var curentPrice = parseFloat(object.price); var priceMinOk = true; var priceMaxOk = true; // filter results match the price range if(maxPrice || minPrice){ if(maxPrice && maxPrice<curentPrice){ priceMaxOk = false; } if(minPrice && minPrice>curentPrice){ priceMinOk = false; } } // loop over list and get only related to new array if (priceMinOk && priceMaxOk) { a = os.match("android"); i = os.match("ios"); o = os.match("other"); ls = os.match("ls"); if (a == "android" || i == "ios" || o == "other" || ls == "ls") { if (object.os == a || object.os == i || object.os == o || object.lsw == ls) { //filterdProducts[key] = object; // key++; var found =


.map(filterdProducts, function (val){
return val.os == ios,val.lsw == ls;
});


}

}
其他 {
filterdProducts [key] =对象;
key ++;
}


}



});
}
.map(filterdProducts, function (val) { return val.os == "ios", val.lsw == "ls"; }); } } else { filterdProducts[key] = object; key++; } } }); }


HÍow根据什么标准得到数组

假设您有一个想要过滤的原始数组。你最好使用LINQ。是的,您也可以在JavaScript中使用LINQ。请看这里: http://linqjs.codeplex.com/ [ ^ ]
HÍow to get the array from what, based on what criteria?
Let's suppose you have an original array you want to filter. You better use LINQ. Yes, you can have LINQ also in JavaScript. Look here: http://linqjs.codeplex.com/[^]


这篇关于在jQuery中基于两个变量进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆