Javascript数组包含键为字符串或字符串数组的位置

Javascript数组包含键为字符串或字符串数组的位置,javascript,arrays,lodash,Javascript,Arrays,Lodash,我有一个数组,看起来像: var data = [{ key: ['blue-berries', 'strawberries'], title: 'Berries', }, { key: 'chicken', title: 'Chicken', }, { key: 'beef', title: 'Beef', }, { key: 'something-else', title: 'Something Else', }] 假设我的“

我有一个数组,看起来像:

var data = [{
    key: ['blue-berries', 'strawberries'],
    title: 'Berries',
}, {
    key: 'chicken',
    title: 'Chicken',
}, {
    key: 'beef',
    title: 'Beef',
}, {
    key: 'something-else',
    title: 'Something Else',
}]
假设我的“查询”类似于“/草莓和其他东西”

我们应该返回示例数组中的第一项

“/和其他任何东西一起吃牛肉” 将返回第三个,以此类推

如您所见,一些键属性是数组,另一些只是静态字符串

使用lodash\ux.find,以下操作似乎有效

但是,这是一个误报,好像我将输入更改为“/beef with somether whates”,它仍然返回数组中的第一项

我想我是误用了uu。不知怎的。。。
有更好的办法吗?

我不知道用什么样的方式。我想你可以用原生js来做

var str = "/beef-with-some-other-whatever";
var obj = data.filter(function(o){
    return str.match(/\w+/g).some(function(word){ 
       ~(word+"").indexOf(word);
    });
})[0];

请注意,若单词恰好是数组,它将调用join,所以它仍然可以工作。

我不知道lodash、下划线或其他什么。我想你可以用原生js来做

var str = "/beef-with-some-other-whatever";
var obj = data.filter(function(o){
    return str.match(/\w+/g).some(function(word){ 
       ~(word+"").indexOf(word);
    });
})[0];
var input = '/strawberries-with-some-other-stuff';

var result = _.find(data, function (el) {
  if (Array.isArray(el.key)) {
    return _.find(el.key, function (k) {
      return input.indexOf(k + '-') >= 0;  // or return input.indexOf(k + '-') == 0; - need replace first symbol '/' 
    });
  }

  return input.indexOf(el.key + '-') >= 0;  // or return input.indexOf(el.key + '-') == 0; - need replace first symbol '/' 
});
注意,若单词恰好是数组,它将调用join,所以它仍然可以工作

var input = '/strawberries-with-some-other-stuff';

var result = _.find(data, function (el) {
  if (Array.isArray(el.key)) {
    return _.find(el.key, function (k) {
      return input.indexOf(k + '-') >= 0;  // or return input.indexOf(k + '-') == 0; - need replace first symbol '/' 
    });
  }

  return input.indexOf(el.key + '-') >= 0;  // or return input.indexOf(el.key + '-') == 0; - need replace first symbol '/' 
});
演示:


演示:

您在这里遗漏了几点:

您正在从forEach回调中返回一个值。这没有任何意义,对任何事情都没有任何影响。 传递给u.find的函数应该是谓词。这意味着它返回true或false。 以下内容适用于您的要求:

var input = '/strawberries-with-some-other-stuff';

var result = _.find(data, function(d) {
    if (d.key instanceof Array) {
        return !!_.find(d.key, function(key) {
            return input.indexOf(key + '-') === 0;                
        });
    } 

    return input.indexOf(d.key + '-') === 0;
});
这里有一些重复的代码,因此您可以进一步简化:

var input = '/strawberries-with-some-other-stuff';

var result = _.find(data, function(d) {
    function matchesQuery(keyStr) { return input.indexOf(keyStr + '-') === 0; } 

    return (d.key instanceof Array)
        ? !!_.find(d.key, matchesQuery)
        : matchesQuery(d.key);
});

您在这里遗漏了几点:

您正在从forEach回调中返回一个值。这没有任何意义,对任何事情都没有任何影响。 传递给u.find的函数应该是谓词。这意味着它返回true或false。 以下内容适用于您的要求:

var input = '/strawberries-with-some-other-stuff';

var result = _.find(data, function(d) {
    if (d.key instanceof Array) {
        return !!_.find(d.key, function(key) {
            return input.indexOf(key + '-') === 0;                
        });
    } 

    return input.indexOf(d.key + '-') === 0;
});
这里有一些重复的代码,因此您可以进一步简化:

var input = '/strawberries-with-some-other-stuff';

var result = _.find(data, function(d) {
    function matchesQuery(keyStr) { return input.indexOf(keyStr + '-') === 0; } 

    return (d.key instanceof Array)
        ? !!_.find(d.key, matchesQuery)
        : matchesQuery(d.key);
});

单词之间会用-?是否应该测试indexOf==1以查找/或者我是否遗漏了什么?@AmitJoki-通常,但这不可能guaranteed@Hacketo-好的观点-在我的实际应用程序中,我用这个代码替换“/”,并用indexOf==1和/beef与其他一些代码一起工作。这些单词之间会用什么分隔-?应该测试indexOf==1以确定/还是我遗漏了什么?@AmitJoki-通常,但这不可能guaranteed@Hacketo-很好-在我的实际应用程序中,我用以下代码替换“/”,这段代码对我有用,indexOf==1,而/beef则是其他任何东西