Javascript如何过滤匹配关键字的数组 [英] Javascript how to filter arrays matching a keyword

查看:41
本文介绍了Javascript如何过滤匹配关键字的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个深度嵌套的数组,如下所示:

I have a deeply nested array as follows:

{[{
    id: "1",
    experts: [
        {instruments: [{guitar: ["John", "Maria"], 
                         piano: ["Hans", "Sarah", "Nancy"] }],
         name: "Columbia Records", published: "1930"
        },
        {instruments: [{guitar: ["Pablo"], 
                        drums: ["Jeb", "Xiao"]} ],
         name: "Atlas Records", published: "1978"
        }
    ]
]},
{
    id: "2",
    experts: [
        {instruments: [{sitar: ["Anaka", "Reha"], 
                        chello: ["Hans", "Raphael", "Stuart"]} ],
         name: "Touchstone Records", published: "1991"
        },
        {instruments: [{viola: ["Richard", "Farah"], 
                        buss: ["Tina", "Max"]} ],
         name: "Atlas Records", published: "1978"
        }
    ]

}

我需要执行分级搜索.例如,如果我的搜索键是Jo",结果应该是:

I need to perform leveled search. For example if my search key is "Jo" the result should be:

[{
        id: "1",
        experts: [{"instruments": [
                                   "guitar": ["John"]
                                  ]              
                 ],
             "name": "Columbia Records", "published": "1930"
 }]

换句话说,每个嵌套级别,我只是根据键进行过滤,如果它不存在,则删除嵌套对象并进一步.如果搜索键是1",它将返回整个第一个对象.

In other words each nested level, I just filter based on the key and if it does not exist then remove the nested object and go further. If the search key is "1" it will return the whole first object.

我正在研究 loadsh 及其过滤器函数,它采用谓词但不确定如何实现搜索任意嵌套.

I am looking into loadsh and its filter function which takes a predicate but not sure how to achieve the search arbitrary nesting.

推荐答案

在这里看起来很有趣.首先你的对象声明有很多错误.您缺少结束标记,并且您没有使用 ["key":"value"] 之类的键创建数组,这是用于对象 {"key":"value"}

It looked like fun so here. To begin with your object declaration had lots of errors. You were missing closing tags, and you don't create arrays with keys like ["key":"value"], that's for objects {"key":"value"}

var testArray = [{
    id: "1",
    experts: [
        {"instruments": {"guitar": ["John", "Maria"], 
                         "piano": ["Hans", "Sarah", "Nancy"] },
         "name": "Columbia Records", "published": "1930"
        },
        {"instruments": {"guitar": ["Pablo"], 
                         "drums": ["Jeb", "Xiao"] },
         "name": "Atlas Records", "published": "1978"
        }
    ]
},
{
    id: "2",
    experts: [
        {"instruments": {"sitar": ["Anaka", "Reha"], 
                         "chello": ["Hans", "Raphael", "Stuart"] },
         "name": "Touchstone Records", "published": "1991"
        },
        {"instruments": {"viola": ["Richard", "Farah"], 
                         "buss": ["Tina", "Max"] },
         "name": "Atlas Records", "published": "1978"
        }
    ]
}];

一种方法是使用递归,例如:

One way is to use recursion, something as such:

function deepSearchForValue(obj, searchstring) {
  var doesValueExist = function(item) {
    if (typeof item === "string") {
      return item.toLowerCase().indexOf(searchstring.toLowerCase()) >= 0;
    } else {
      return doesValueExistRecursive(item);
    }
  }

  var doesValueExistRecursive = function(item) {
     for (i in item) { 
        if (doesValueExist(item[i])) {
           return true;
        }
     }
     return false;
  }

  return obj.filter(doesValueExistRecursive);
}

你会这样称呼它

var matchedItems = deepSearchForValue(testArray, "reh");

console.log("found:");
console.log(matchedItems);

这篇关于Javascript如何过滤匹配关键字的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆