I\'m running an express.js app that has a few apis feeding data to dropdown boxes. The data returned is in the form:
[
{
key: \'blah\',
You could extract the values via map, and form them into a regex to match values against.
Example: http://repl.it/X0V
var items=
[
{
key: 'blah',
value: 'Blah Blah'
},
{
key: 'foo',
value: 'Foos'
},
{
key: 'bar',
value: 'Bars'
},
{
key: 'baz',
value: 'Bazingo'
}
];
var toReg = items.map(function(obj){
return obj.key;
}).join('|');
var regex = new RegExp('^('+ toReg +')$');
//To test the regex
var itemsToTest = ['blah', 'Pies', 'foo', 'Bazingo'];
itemsToTest.forEach(function(key){
if(regex.test(key)){
console.log(key);
}
});