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