Skip to content
Advertisement

How do I search through an array using a string, which is split into an array?

I currently have a half solution to this, but I was wondering if there is a better way to write this. I have the following array with equipment deliveries:

JavaScript

Which I search through using an input:

JavaScript

What I’m trying to achieve is to split the search word with spaces, and check if an index in the array contains all of these words. I currently search up to three words, since the code would become too long using my “solution”, if I searched for more. This is my code:

JavaScript

What would be the correct way to search using all the words in the pn array?

I tried saving the if statements as a string, adding code to the string for each index of pn and then using eval. This turned out to slow the search to a crawl though.

Any help would be appreciated.

Example added for a user searching for “cub a32”:

JavaScript

Which turns into:

JavaScript

Results in:

JavaScript

Advertisement

Answer

You could filter the array and check with every or some, depending if you want all or just one search string in an item of deliveries.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement