1

Lets say I have a search box and whatever is typed in it is stored as:

var search_input = $(this).val();

How would I search my array with this value?

var devices = [{"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},{"Device_ID":"44","Image":"Apple-iPad.png","Manufacturer":"Apple","Model":"iPad","Type":"Tablet"}]

So lets say I had Amazon, or Kindle etc in my search_input variable, I would want it to search the array and output the below. The same would apply for any thing typed or anything in the array

<div id="device">43</div>
<div id="image">Amazon-Kindle-Fire.png</div>
<div id="name">Amazon Kindle Fire</div>
1
  • To achieve what you want, I would loop through the array using $.each function as well as the JavaScript indexOf function to search the string. The property of the object I would use the indexOf function would be the "name" property. Can give an example if you tell me how you want to output the result? I.e. Do you want me to give an example using Jquery Templates to output the result if a match is found? Commented Oct 5, 2012 at 14:22

4 Answers 4

6

Simple loop in a loop

var devices = [{"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},{"Device_ID":"44","Image":"Apple-iPad.png","Manufacturer":"Apple","Model":"iPad","Type":"Tablet"}];

function findItem (term) {
    var items = [];
    for (var i=0;i<devices.length;i++) {
        var item = devices[i];
        for (var prop in item) {
            var detail = item[prop].toString().toLowerCase();
            if (detail.indexOf(term)>-1) {
                items.push(item);
                break;               
            }
        }
    }
    return items;
}

console.log( findItem("amazon") );  //returns one item
console.log( findItem("kindle") );  //returns one item
console.log( findItem("apple") );   //returns one item
console.log( findItem("tablet") );  //returns two items
console.log( findItem("nerd") );    //returns zero items
​

jsFiddle

Sign up to request clarification or add additional context in comments.

5 Comments

How would I tie my var search_input = $(this).val(); value into it to search and output the details as mentioned?
You do not know how to access an object to produce the output? var result = findItem("amazon"); alert(result[0].Image);
Oh yeah of course - sorry long day! So if I try to get the Model instead and use alert(result[0].Model); the console outputs Cannot read property 'Model' of undefined
why don't we just use grep and save some lines of code?
@DhruvSinghal well in 2012 this was a good answer, in this day in age filter() would be the way to go.
4

I think a more elegant way to achieve the goal in this question would be to use jQuery.grep() -- see http://api.jquery.com/jquery.grep/

var search_input = $(this).val();
var devices = [
       {"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},
       {"Device_ID":"44","Image":"Apple-iPad.png","Manufacturer":"Apple","Model":"iPad","Type":"Tablet"}
];

var matchingItems = $.grep(devices, function(e) {
            return (e["Manufacturer"].indexOf(search_input) > -1 ||
                    e["Model"].indexOf(search_input) > -1);
});

Now matchingItems contains an array of items that have search_input word in Manufacturer or Model properties, and you can output those in html.

2 Comments

what is 'term' here?
@DhruvSinghal, term was a typo, should be search_input, updated the answer
1

I think you need:

jQuery.inArray()

http://api.jquery.com/jQuery.inArray/

2 Comments

But it is an Array of objects
You are right!, sorry, in that case you can use "each". $.each( devices , function(){ if ($(this).device == 43) //output } )
1

Another way would be to replace your array with a map (JavaScript object) where the key is the value you want to compare with the search_input. That way you simply look up the key and get the object.

var devices = {"Amazon Kindle": {"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},{"Device_ID":"44","Image":"Apple-iPad.png","Manufacturer":"Apple","Model":"iPad","Type":"Tablet"}}

The second step involves converting the JavaScript object to XML, which is already answered on SO here What's the best way to serialize JavaScript objects to XML?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.