How to return result from JSON using case insensitive search using javascript?
My JSON contains "products", each with multiple meanings (name, manufacturer, etc.).
I have the following code that allows me to pull "products" from my JSON based on the search result I got from the query string. It works great, but only if the search entry is formatted exactly as it is written in the string.
How can I resolve case insensitive searches, gives the desired result (and ideally a partial search)?
I believe regexes are the way to go, but I've exhausted my knowledge and can't work with anyone with it.
I tried to put together a fiddle but couldn't get a proper demo, so instead I tried to consolidate the code and comment it out for clarity. Any help would be greatly appreciated :)
Parse the JSON:
var prodobjects = JSON.parse(prods);
Code to get products based on a specific value:
function getData(array, type, val) {
return array.filter(function (el) {
return el[type] === val;
});
}
Code to extract the query string:
function gup( name ){
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null ) return "";
else return results[1];}
The section of the query string that I want to use:
var prodresult = gup( 'search' );
Strip the pluses and replace them with spaces:
var removeplus = prodresult.replace(/\+/g, ' ');
Compile a list of products with their "Prodname" matching the "search" query:
var searchname = getData(prodobjects.products, 'Prodname', removeplus);
And as spelled out here is a sample JSON. It is still in development, so null values etc. are currently being developed. (It is obtained via api). This is just one of the products, but the actual string contains many in one format (but inside "products"):
var prods = JSON.stringify({"products": [
{
"Prodname": null,
"Oem": "Example OEM",
"Snippet": "Example Snippet",
"Linkto": "www.linkhere.com",
"Imagesource": "image.png",
"Category": "Category",
"Tagline": "Tagline goes here",
"Longdescription": [
{
"Paragraph": "<p>First description of the paragraph</p>"
},
null,
null,
null
],
"Features": null,
"Company": false,
"Subscribed": false,
"Tariffs": [
{
"Tarname": "Tariff one",
"Tarpaysched": "Monthly per User",
"Tarcost": "£1"
},
null,
null,
null,
null,
null
],
"Extratariffs": null
}
]
});
--- UPDATE ---
I managed to get it working to support partial search and case insensitivity with the following:
function getData(array, type, val) {
return array.filter(function (el) {
if (el[type]!=null && val!=null) {
var seeker = val.toLowerCase();
var rooted = el[type].toLowerCase();
var boxfresh = rooted.indexOf(seeker);
if (boxfresh!=-1) {
return rooted
}
}
});
}
source to share
You can convert two strings to lower case (or upper case) to make the comparison case insensitive.
function getData(array, type, val) {
return array.filter(function (el) {
return el[type].toLowerCase() === val.toLowerCase();
});
}
For a better search, you may want to look at fuzzy comparison , which is a "search across the data to determine probable wrong matches and approximate string matches."
source to share