javascript - Multiple keyword (that has regex) search through JSON -



javascript - Multiple keyword (that has regex) search through JSON -

i have json looks like:

"groups": [ "group_id": "8", "group_name": "building", "group_color": "00ff00" }, { "group_id": "3", "group_name": "building", "group_color": "8000ff" }, { "group_id": "2", "group_name": "sidewalk", "group_color": "ff0000" }, { "group_id": "6", "group_name": "parking lot", "group_color": "00ffff" }, { "group_id": "3", "group_name": "commons", "group_color": "ff8000" }, { "group_id": "5", "group_name": "other", "group_color": "ff00ff" } ]

and when field found flips boolean flag. e.g.

for (var c=0; c<json.groups.length; c++) { insearch = false; if(json.groups.group_id.match(query)!=null){ insearch = true; } }

however, if query = building homecoming 2 results, i'm looking set query = building&8 homecoming first result.

note: query can regular look think should go...

any ideas?

edit: there way to, say, split string parts , match single object based on 2 different pieces of object? match first object based on either group_id , group_color, or match based on group_name , group_color?

for nowadays case should work:

data = json.parse('your_json'); (var i=0, j=data.length; i<j; i++){ var insearh = false; if(data[i].group_id===8 && data[i].group_name==="something"){ insearch = true; break; } }

for more general case can wrap in function:

key_tester = function(jsondata,key1, value1, key2, value2){ // here jsondata parsed json for(var i=0, j=jsondata.length; i<j; i++){ var obj = jsondata[i]; if(obj[key1] == value1 && obj[key2] == value2){ homecoming true; } homecoming false; }

then can utilize like:

jsondata = json.parse('your json'); if (key_tester(jsondata, "group_id","8","group_name","building")) // here goes awesome code

you can extend key_tester function include more key value pairs.

this should save going regex way.

javascript json search

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -