【发布时间】:2021-04-04 05:44:48
【问题描述】:
在我的 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 做到这一点?我需要搜索 IN 并在对象中的 2 个字段中搜索。谢谢;)
【问题讨论】:
-
我猜你不需要 lodash,尝试使用过滤器,例如:articleArray.filter(obj => obj.productName.includes("4") || obj.article .includes("4"))
标签: javascript angular lodash