JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 6th, 2012, 05:12 AM
Evostance Evostance is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 55 Evostance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 35 m 45 sec
Reputation Power: 6
Question Search through JSON Array using Javascript 'like' operator?

So here's my problem.
I have an search box which has the JavaScript variable search_input assigned to it.

I've then got my JSON Encoded Array named devices as so:

Code:
    var devices = [{"Device_ID":"43","Image":"Amazon-Kindle-Fire.png","Manufacturer":"Amazon","Model":"Kindle Fire","Type":"Tablet"},{"Device_ID":"69","Image":"Asus-Nexus-7.png","Manufacturer":"Asus","Model":"Nexus 7 ","Type":"Tablet"},{"Device_ID":"40","Image":"Asus-Transformer.png","Manufacturer":"Asus","Model":"Transformer","Type":"Tablet"},{"Device_ID":"41","Image":"Asus-Transformer-Prime.png","Manufacturer":"Asus","Model":"Transformer Prime","Type":"Tablet"}]


On my page, I have some more JavaScript which reads through the array and writes it to a page as so:

Code:
     	for(var i=0;i<devices.length;i++){
        		    var device_id = devices[i].Device_ID;
	        		var image = devices[i].Image;
	        		var manufacturer = devices[i].Manufacturer;
	        		var model = devices[i].Model;
	        		var type = devices[i].Type;
	        		
	        		document.write(+device_id+'-'+image+'-'+manufacturer+'-'+model+'-'+type+);
		}


This so far works great, it shows everything in the array, just how I want it.

What I need to do now though, is when someone types something into the search_box, I need it to effectively filter the array by the concatenation of Manufacturer & Model by what they have searched for so the JavaScript above, only shows the array content of what they are searching.

The other issue is that it needs to be a 'LIKE' statement as you would have it in SQL. So if a user searched from Ama it would still return Amazon Kindle Fire, 7 would bring back Asus Nexus 7, N would bring back everything with N in it etc.

Reply With Quote
  #2  
Old October 6th, 2012, 11:52 AM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 43 m 47 sec
Reputation Power: 6991
JavaScript doesn't have a LIKE operator, but you can achieve a similar result by simply searching the string to see whether it contains the search term. For example:
Code:
(string_to_test.indexOf(search_term) !== -1)


If that statement is true, then search_term is contained within string_to_test; otherwise it is not. This would be similar to doing:
Code:
column_to_test LIKE '%search_term%'

in a SQL query.
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Search through JSON Array using Javascript 'like' operator?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap