/    Sign up×
Community /Pin to ProfileBookmark

Javascript Text Search of JSON File

I have a multi node JSON list. It’s a list of people. Firstname, last name, middle name, knownby, dob etc.

I have a user input field which a user, uses to search for people.

Easy if you spell the name correctly.

But I have is a large number long and double barreled names. Such as ‘Barry John Mark Stevenson’, where Barry and John are both first legal names so will be in the firstname node.

How can I search for BarryJohn, Barry, John etc.

I also want my search input to check other name nodes and return valid options.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@kiwisauthorOct 09.2021 — Okay, here's where I'm at. Outside this code, I have an event listener listening for a keyup event on an input box.

That fires the input into this function, value is the user input & data is my JSON array.

``<i>
</i>function searchUser(value, data){
var filteredData = []
for (var i = 0; i &lt; data.length; i++){
var playerSurname = data[i].userFirstname.toLowerCase()
var playerFirstname = data[i].userLastname.toLowerCase()
var knownAs = data[i].userKnownas.toLowerCase()
var nickname = data[i].userNickname.toLowerCase()

if (userSurname.includes(wrd)){
filteredData.push(data[i])
} else if (userFirstname.includes(wrd)){
filteredData.push(data[i])
} else if (userknownAs.includes(wrd)){
filteredData.push(data[i])
} else if (userNickname.includes(wrd)){
filteredData.push(data[i])
}
}
return filteredData
}<i>
</i>
`</CODE>

This works when I look up Michael. But if I look up Michael Jones - I would like it to refine based on the second word.

I was looking to use

<CODE>
`<i>
</i> var words = value.split(' ');
words.forEach((wrd) =&gt;{<i>
</i>
``


But not sure how I can cut back filteredData the second, third time?
Copy linkTweet thisAlerts:
@boohooOct 09.2021 — How about this?

``javascript<i>
</i>const words = value.trim().toLowerCase().split(/s+/);

const properties = [
'userFirstname',
'userLastname',
'userKnownas',
'userNickname',
];

const results = data.filter(
entry =&gt; words.every(
word =&gt; properties
.map(
property =&gt; entry[property].toLowerCase()
)
.includes(word)
)
);<i>
</i>
`</CODE>
I used <C>
.every()</C>, so it only includes the results where all words are found somewhere in <C>data</C>. You can change it to <C>.some()</C> if you want those where <EM>*any*</EM> word is matched.

Also, please notice how we avoided spaghetti here. You can easily and and remove <C>
properties` to be included in your searching.
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...