I have an array which looks like:
var data = [{
key: ['blue-berries', 'strawberries'],
title: 'Berries',
}, {
key: 'chicken',
title: 'Chicken',
}, {
key: 'beef',
title: 'Beef',
}, {
key: 'something-else',
title: 'Something Else',
}]
Assuming my 'query' was something like '/strawberries-with-some-other-stuff'
We should return the first item in the example array.
'/beef-with-some-other-whatever'
Would return the 3rd (and so on)
As you can see, some key properties are arrays, others are just static strings
The following appears to work, using lodash _.find
var input = '/strawberries-with-some-other-stuff';
var result = _.find(data, function(d) {
if (d.key instanceof Array) {
return _.forEach(d.key, function(key) {
if (input.indexOf(key + '-') == 0) {
console.log('key- ', key + '-')
return d
} else return null;
});
} else {
if (input.indexOf(d.key + '-') == 0) {
return d
}
}
});
However, it's a false positive, as if I change input to '/beef-with-some-other-whatever' it still returns the first item in the array
I presume I'm misusing _.find somehow... Is there a better way?
-?