0

I am creating a search filter for my array of objects so let's say I have an array of object

let arr = [{'name': 'mn zx abc'}, {'name': 'zx mn'}, {'name': 'mnzx'}]

now if i search for zx than it should return me result like

[{'name': 'mn zx abc'}, {'name': 'zx mn'}]

note than last object {'name': 'mnzx'} the zx is coming in between so i don't want that, hope you understands my problem

here is the code -

let arr = [{'name': 'mn zx abc'}, {'name': 'zx mn'}, {'name': 'mnzx'}];

let searchedTerm = 'zx';

let result = arr.filter(data => {
    if (data.name.charAt(0) === searchedTerm.charAt(0)) {
        return true;
    }
});

console.log(result);
Fraction
  • 11,668
  • 5
  • 28
  • 48
  • Unfortunately the problem is that you appear to be expecting us to do the coding for you... that's not how stackoverflow works. You need to prove you've tried doing this yourself by providing a [mcve] (with a [snippet](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-create-a-runnable-example-with-stack-snippets-how-do-i-do) if possible) in your question. Please have a read of the [help] and the [ask] section in particular – freefaller Jun 27 '19 at 11:06
  • Please try to upload the code which you have tried. thanks – Shivani Sonagara Jun 27 '19 at 11:06
  • Possible duplicate of [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – James Jun 27 '19 at 11:44

4 Answers4

0

It looks like you are trying to identify objects where the name string contains a particular "word" as opposed to just particular characters.

One simple way to approach this would be to split the name field into words and then check the split list for the search term. If your words are delimited by whitespace, you could do "mn zx abc".split(/\s+/) for example to get ["mn", "zx", "abc"]. /\s+/ is a regular expression which matches one or more whitespace characters, so splitting on this leaves us with just the groups of non-whitespace characters.

We can then check for the search term's presence in the list using indexOf.

Here's a full example:

function matches(obj, searchTerm) { return obj.name.split(/\s+/).indexOf(searchTerm) >= 0; }

let arr = [{'name': 'mn zx abc'}, {'name': 'zx mn'}, {'name': 'mnzx'}];
let result = arr.filter(obj => matches(obj, "zx"));
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
  • hey. what happens if the searched term is 'mn' and the code which you shows will work for that also if the name is 'mnzx' so it should also include that name, where in your code its not including – Shailesh Gehlot Jun 28 '19 at 06:06
  • I want a solution like in our android phone we can search a contact from the first name or last name so let's say I have saved a contact name like 'Shailesh Gehlot' so if I search like 'sha' or 'geh' it should show me 'Shailesh Gehlot' contact, whereas if i have contact name 'brashae mishra' even if searched term 'sha' is there in this name it should not include that name and do not show me that contact. let me know if u want me to elaborate more. thanks – Shailesh Gehlot Jun 28 '19 at 06:15
  • @ShaileshGehlot any permutation of this kind of functionality can be pretty straightforward, but it's hard to tell the precise behavior you want from your examples (for example, do you want a search of "geh sha" to return "Shailesh Gehlot" or not? Do you want case-sensitivity? My suggestion would be to break the problem down and just focus on writing a matches function like the one above that fits the logic you're imagining. – ChaseMedallion Jul 01 '19 at 01:02
0

I'd approach it using a regular expression...

// Your original array
let arr = [{'name': 'mn zx abc'}, {'name': 'zx mn'}, {'name': 'mnzx'}]

// Your original search term
let searchedTerm = 'zx';

// A new RegExp object which looks for your search term with a space before it (with case-insensitivity)
let searchRE = new RegExp(" " + searchedTerm, "i");

let result = arr.filter(data => {
    // Test to see if your data.name (with an additional space before it) matches
    return searchRE.test(" " + data.name);
});
console.log(result);
freefaller
  • 19,368
  • 7
  • 57
  • 87
0

You can use regular expression with word boundary in array#filter to get all name with zx.

let arr = [{'name': 'mn zx abc'}, {'name': 'zx mn'}, {'name': 'mnzx'}],
    searchedTerm = 'zx',
    result = arr.filter(({name}) => new RegExp( `\\b${searchedTerm}\\b`, "i").test(name));
console.log(result);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

You can filter the array using RegExp.test() with word boundary (\b):

let arr = [{'name': 'mn zx abc'}, {'name': 'zx mn'}, {'name': 'mnzx'}, {'name': 'zxmn'}, {'name': 'abczx mn'}];

const output = arr.filter(obj => /\bzx\b/.test(obj.name));

console.log(output);
Fraction
  • 11,668
  • 5
  • 28
  • 48