I have some JSON that 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 a field is found it flips a 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 then I return two results, so what I’m looking to do is set query = Building&8 to return only the first result.
Note: query can also be a regular expression which I think is where this should go…
Any ideas?
EDIT: Is there a way to, say, split my string into parts and then match a single object based on two different pieces of that object? So match the first object based on either group_id and group_color, or match it based on group_name and group_color?
For present case something like this should work:
For a more general case you can wrap it in a function:
Then you can use it like:
You can extend the key_tester function to include more key value pairs.
This should save us from going the regex way.