lodash 在对象数组中搜索文本
lodash search text in object array
在我的 angular 应用程序中,我有搜索输入
<input
id="search"
type="text"
placeholder="search"
class="form-control"
formControlName="search"
(keydown)="mySearch()"
>
函数是:
mySearch() {
// search code here
}
这是我需要搜索的数组
[
{
"_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 和 article.. 中包含字符串包含“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 做到这一点?我需要类似 search IN 和 search in 2 fields in object 的东西。谢谢 ;)
您可以使用此代码:
array.filter(a=> _.values(a).some(b => b.includes("4")));
_.values 是一个 lodash 方法
在我的 angular 应用程序中,我有搜索输入
<input
id="search"
type="text"
placeholder="search"
class="form-control"
formControlName="search"
(keydown)="mySearch()"
>
函数是:
mySearch() {
// search code here
}
这是我需要搜索的数组
[
{
"_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 和 article.. 中包含字符串包含“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 做到这一点?我需要类似 search IN 和 search in 2 fields in object 的东西。谢谢 ;)
您可以使用此代码:
array.filter(a=> _.values(a).some(b => b.includes("4")));
_.values 是一个 lodash 方法