Click here to Skip to main content
15,992,684 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can I get the following array based on two variables simultaneously search?

JavaScript
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.
JavaScript
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++;
                    }

                    
                    }
                
                

            });
        }
Posted

1 solution

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/[^]
 
Share this answer
 
Comments
King Fisher 27-Dec-14 16:26pm    
wow...Thank you 5+
Member 10418736 27-Dec-14 16:31pm    
Yes. But I do not know the link.
If it's a way I can do it yourself ?
Thank
Zoltán Zörgő 27-Dec-14 16:49pm    
Of course. But tell us the logic you tried to implement.

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