3

In my angular app I have search input

 <input
    id="search"
    type="text"
    placeholder="search"
    class="form-control"
    formControlName="search"
    (keydown)="mySearch()"
  >

function is:

mySearch() {
  // search code here
}

This is array where I need to search

[
{
    "_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",
}
]

when I type "4" as result I need to have all object where string include "4" in productName and article.. like this:

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

{
    "_id": "7",
    "productName": "test name 7",
    "article": "4d",
}
]

How I can do that using lodash? I need something like search IN and search in 2 fields in object. Thanks ;)

1
  • I guess that you don't need lodash for that, try to use a filter, like: articleArray.filter(obj => obj.productName.includes("4") || obj.article.includes("4")) Commented Apr 4, 2021 at 6:14

1 Answer 1

1

you can use this code:

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

_.values is a lodash method

Sign up to request clarification or add additional context in comments.

3 Comments

hey, Alix. thank you very much, but I I get error core.js:4352 ERROR TypeError: b.includes is not a function
because I supposed to type of all value store in object were string. if you use other types you have to use own methods to Comparison
Thank you very mush again! I will create array with only string types and then use your code. Thank You!!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.