Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How jQuery in an array can be searched based on multiple values?
For example, I search by color and os
Thank
JavaScript
var products = [
                     {"id":"1","title":"Apple iPhone 4","os":"ios","color":"red"} 
                     ,{"id":"2","title":"Blac80 Bold","os":"other","color":"blue"} 
                     ,{"id":"12","title":"Black 0","os":"other","color":"red"} 
                     ,{"id":"26","title":"HTC s","os":"android","color":"blue"} 
                     ,{"id":"50","title":"HTC HD7","os":"android","color":"red"} 
                     ,{"id":"21","title":"HTC z","os":"android","color":"blue"} 
                     ,{"id":"11","title":"HTC ion","os":"android","color":"red"} 
                     ,{"id":"3","title":"HTC W","os":"android","color":"blue"} 
                     ,{"id":"6","title":"Motorola","os":"ios","color":"blue"} 
                     ,{"id":"14","title":"Motorola DEFY","os":"ios","color":"red"} 
                     ,{"id":"15","title":"Nokia C7","os":"ios","color":"blue"} 
                     ,{"id":"60","title":"Nokia N8","os":"ios","color":"red"} 
                     ,{"id":"70","title":"Nokia X6","os":"ios","color":"blue"} 
                     ];
Posted

1 solution

To get all Nokia N8 in red:

JavaScript
$.grep(products, function(n, i) {
return n.color == "red" && n.os == "Nokia N8";
});


In the anonymous function, n is the item in the array; and i is its index. In other words, Nokia N8 red is actually at index 11, so we could have easily have said:

XML
$.grep(products, function(n, i) {
if(i == 11) {
return true;
}
});



http://api.jquery.com/jquery.grep/[^]
 
Share this answer
 
v2
Comments
Member 10418736 2-Jan-15 10:55am    
I would use this method.
If this was the explanation for this?

var filterdProducts = []; // displayed products array
var key = 0;
filterdProducts = products;
if (a == "android" || i == "ios" || o == "other" || colors == "red") {

if (object.os == a || object.os == i || object.os == o || object.color == colors) {

filterdProducts[key] = object;
key++;
}
}
else {
filterdProducts[key] = object;
key++;
}
Doug Vanderweide 2-Jan-15 12:59pm    
The logic you have now won't work.

If what you want is to be able to pass, to a function, arrays of OSes and colors, and get back every match that meets those arrays:

var filterColor = ["red"];
var filterOS = ["android", "ios", "other"];

var filteredProducts = $.grep(products, function(n, i) {
return $.inArray(n.os, filterOS) && $.inArray(n.color, filterColor);
});


filteredProducts is now an array of JSON objects that are red in color, and have an OS of android, ios or other.
Member 10418736 2-Jan-15 14:36pm    
Thanks.
All of my code

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");
colors = os.match("red");


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

if (object.os == a || object.os == i || object.os == o || object.color == colors) {

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

I let the check box to search by color , os
Member 10418736 2-Jan-15 15:00pm    
This link is a project
https://www.transferbigfiles.com/0d8678bd-05b1-4a48-98bc-ac53767c0634/-br17aOg-wowmBQ_H2JVBA2
I thank check

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900