Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对象数组中的lodash搜索文本_Javascript_Angular_Lodash - Fatal编程技术网

Javascript 对象数组中的lodash搜索文本

Javascript 对象数组中的lodash搜索文本,javascript,angular,lodash,Javascript,Angular,Lodash,在我的angular应用程序中,我有搜索输入 <input id="search" type="text" placeholder="search" class="form-control" formControlName="search" (keydown)="mySearch()" > 这是我需要搜索的数组

在我的angular应用程序中,我有搜索输入

 <input
    id="search"
    type="text"
    placeholder="search"
    class="form-control"
    formControlName="search"
    (keydown)="mySearch()"
  >
这是我需要搜索的数组

[
{
    "_id": "1",
    "productName": "test name 1",
    "article": "11111",
},
{
    "_id": "2",
    "productName": "test name 2",
    "article": "11111",
},
{
    "_id": "3",
    "productName": "test name 3",
    "article": "4",
},
{
    "_id": "4",
    "productName": "test name 4",
    "article": "11111",
},
{
    "_id": "5",
    "productName": "test name 5",
    "article": "111111",
},
{
    "_id": "6",
    "productName": "test name 6",
    "article": "11111111",
},
{
    "_id": "7",
    "productName": "test name 7",
    "article": "4d",
}
]
当我键入“4”作为结果时,我需要在productName和文章中包含字符串“4”的所有对象。。像这样:

[
{
    "_id": "3",
    "productName": "test name 3",
    "article": "4",
},
{
    "_id": "4",
    "productName": "test name 4",
    "article": "11111",
},

{
    "_id": "7",
    "productName": "test name 7",
    "article": "4d",
}
]
我如何使用lodash做到这一点?我需要像搜索和搜索在2个领域的对象。谢谢;)

您可以使用以下代码:

array.filter(a=> _.values(a).some(b => b.includes("4")));

_.values是一种lodash方法

我想你不需要lodash,试着使用一个过滤器,比如:articleArray.filter(obj=>obj.productName.includes(“4”)| | obj.article.includes(“4”))嘿,Alix。非常感谢,但我得到了error core.js:4352 error TypeError:b.includes不是一个函数,因为我假设对象中所有值存储的类型都是字符串。如果您使用其他类型,您必须使用自己的方法来进行比较,再次感谢您!我将只创建字符串类型的数组,然后使用您的代码。非常感谢。
array.filter(a=> _.values(a).some(b => b.includes("4")));