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;
if(maxPrice || minPrice){
if(maxPrice && maxPrice<curentPrice){
priceMaxOk = false;
}
if(minPrice && 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 == "other" || ls == "ls") {
if (object.os == a || object.os == i || object.os == o || object.lsw == ls) {
var found = $.map(filterdProducts, function (val) {
return val.os == "ios", val.lsw == "ls";
});
}
}
else {
filterdProducts[key] = object;
key++;
}
}
});
}