I have an array of objects in a known format, it could look like this:
var items = [{id : 1,
desc : "Funny things",
tags : ["Snippet","Funny"],
title : "Awsome"},
{id : 2,
desc : "Hello World",
tags : ["Fun","Funny"],
title : "What"},
{id : 3,
desc : "True story",
tags : ["Snippet","Cool"],
title : "Is it possible with things?"
}];
I want to create some serach ability in my page that serach for diffrent things inside the items and later display it in some way. Does any one know a plugin that could help me with this?
I was just trying using jQuery grep function and came up with this snippet for my example:
var serach = "things"; // Try to get the tags
var obj = $.grep(items, function(n, i){
// Condition one
if(n.desc.indexOf(serach)>=0)
{
return true;
};
// Condition two
if(n.title.indexOf(serach)>=0)
{
return true;
};
// Condition there
var foundTag = false;
for(var i = 0; i<n.tags.length;i++){
if(n.tags[i].indexOf(serach)>=0)
{
foundTag = true;
return true;
};
}
if(foundTag){return true};
return false;
});
It’s pretty straight forward and works. However it dosn’t solve things like priority diffrent properties. How could add a priority to the function. For example, if the serach expression is found in the title it should apper higher in the “matched” array.
So if anyone have any good input or a good plugin I will find it helpful!
You can use jQuery.each creating and array with a weight for each match and then a sort.
Like this: